Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Graphics.h"
- #include "StdAfx.h"
- #include "GF.h"
- #include <functional>
- #include <vector>
- #include <algorithm>
- #include <bitset>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <string.h>
- #include <random>
- #include <iostream>
- #define all(x) (x).begin(),(x).end()
- #ifndef M_PI
- const double M_PI = 3.1415926535897932384626433832795;
- #endif
- const char CONVEX = 1;
- const char COMPLEX = 2;
- const char NONCONVEX = 3;
- std::mt19937 rng(39);
- class TPoint;
- void DrawLine(TPoint a, TPoint b, const RGBPIXEL& color);
- void DrawLine(int x0, int y0, int x1, int y1, const RGBPIXEL& color);
- bool Intersect(const TPoint& a, const TPoint& b, const TPoint& c, const TPoint& d);
- int SignArea(const TPoint& a, const TPoint& b, const TPoint& c);
- class TPoint {
- public:
- int x, y;
- TPoint(int x = 0, int y = 0)
- : x(x)
- , y(y)
- {
- }
- TPoint(const TPoint& a, const TPoint& b)
- : x(b.x - a.x)
- , y(b.y - a.y)
- {
- }
- double Length() {
- return hypot(x, y);
- }
- TPoint operator + (const TPoint& o) {
- return TPoint(x + o.x, y + o.y);
- }
- TPoint operator - (const TPoint& o) {
- return TPoint(x - o.x, y - o.y);
- }
- bool operator < (const TPoint& o) {
- return x < o.x || x == o.x && y < o.y;
- }
- bool operator == (const TPoint& o) {
- return x == o.x && y == o.y;
- }
- bool operator != (const TPoint& o) {
- return !(*this == o);
- }
- private:
- };
- class TPoint3D {
- public:
- int x, y, z;
- TPoint3D(int x = 0, int y = 0, int z = 0)
- : x(x)
- , y(y)
- , z(z)
- {
- }
- TPoint3D(const TPoint3D& a, const TPoint3D& b)
- : x(b.x - a.x)
- , y(b.y - a.y)
- , z(b.z - a.z)
- {
- }
- double Length() {
- return std::sqrt(x * x + y * y + z * z);
- }
- TPoint3D operator + (const TPoint3D& o) {
- return TPoint3D(x + o.x, y + o.y, z + o.z);
- }
- TPoint3D operator - (const TPoint3D& o) {
- return TPoint3D(x - o.x, y - o.y, z - o.z);
- }
- bool operator < (const TPoint3D& o) {
- if (x != o.x) return x < o.x;
- if (y != o.y) return y < o.y;
- return z < o.z;
- }
- bool operator == (const TPoint3D& o) {
- return x == o.x && y == o.y && z == o.z;
- }
- bool operator != (const TPoint3D& o) {
- return !(*this == o);
- }
- };
- class TPolygon {
- public:
- TPolygon(int n)
- : n(n)
- , p(std::vector<TPoint>(n))
- {
- }
- void SetRandomPoint(int index, int w, int h) {
- if (index >= 0 && index < n) {
- p[index].x = rng() % w;
- p[index].y = rng() % h;
- }
- }
- void SetRandom(int w, int h) {
- for (int i = 0; i < n; i++) {
- SetRandomPoint(i, w, h);
- }
- }
- void Draw(const RGBPIXEL& color) {
- for (int i = 0; i < n; i++) {
- DrawLine(p[i].x, p[i].y, p[(i + 1) % n].x, p[(i + 1) % n].y, color);
- }
- }
- // should be with multithreading
- void Fill(TPoint minPoint, TPoint maxPoint, RGBPIXEL color, const std::function<bool(TPoint)>& f) {
- for (int x = minPoint.x; x <= maxPoint.x; x++) {
- for (int y = minPoint.y; y <= maxPoint.y; y++) {
- if (f({ x, y })) {
- gfSetPixel(x, y, color);
- }
- }
- }
- }
- int Size() const {
- return n;
- }
- TPoint& operator [] (int i) {
- i %= n;
- if (i < 0) i += n;
- return p[i];
- };
- TPoint* begin() {
- return &p[0];
- }
- TPoint* end() {
- return std::next(&p.back());
- }
- bool IsComplex() {
- n = p.size();
- if (n <= 3) {
- return false;
- }
- for (int i = 0; i < n; i++) {
- for (int j = i + 2; j < n; j++) {
- if (j == n - 1 && i == 0) continue;
- if (Intersect(p[i], p[(i + 1) % n], p[j], p[(j + 1) % n])) {
- return true;
- }
- }
- }
- return false;
- }
- bool IsConvex() {
- n = p.size();
- if (n <= 3) {
- return true;
- }
- if (IsComplex()) {
- return false;
- }
- int pos = 0;
- int neg = 0;
- for (int i = 0; i < n; i++) {
- const int s = SignArea(p[(i - 1 + n) % n], p[i], p[(i + 1) % n]);
- if (s > 0) {
- pos++;
- }
- if (s < 0) {
- neg++;
- }
- if (pos > 0 && neg > 0) {
- return false;
- }
- }
- if (pos > 0 && neg > 0) {
- return false;
- }
- return true;
- }
- char Classify() {
- if (IsConvex()) {
- return CONVEX;
- }
- else if (IsComplex()) {
- return COMPLEX;
- }
- else {
- return NONCONVEX;
- }
- }
- void MakeConvex() {
- sort(all(p));
- auto cw = [&](const TPoint & a, const TPoint & b) {
- return a.x * b.y - a.y * b.x;
- };
- auto norm = [&](const TPoint& a) {
- return 1ll * a.x * a.x + 1ll * a.y * a.y;
- };
- p.erase(unique(all(p)), p.end());
- sort(p.begin() + 1, p.end(), [&](TPoint& a, TPoint& b) {
- long long val = cw(a - p[0], b - p[0]);
- if (val != 0) return val < 0;
- return norm(a - p[0]) < norm(b - p[0]);
- });
- const int n = p.size();
- if (n <= 2) {
- this->n = n;
- return;
- }
- std::vector<TPoint> ans = { p[0], p[1] };
- int sz = 2;
- for (int i = 2; i < n; i++) {
- while (sz >= 2 && cw(ans[sz - 1] - ans[sz - 2], p[i] - ans[sz - 1]) >= 0) {
- ans.pop_back();
- sz--;
- }
- sz++;
- ans.push_back(p[i]);
- }
- while (sz >= 2 && cw(ans[sz - 1] - ans[sz - 2], ans[0] - ans[sz - 1]) >= 0) {
- ans.pop_back();
- sz--;
- }
- p = ans;
- this->n = p.size();
- }
- private:
- int n;
- std::vector<TPoint> p;
- };
- void DrawLine(int x0, int y0, int x1, int y1, const RGBPIXEL& color) {
- const int deltaX = abs(x0 - x1);
- const int deltaY = abs(y0 - y1);
- const int signX = x0 < x1 ? 1 : -1;
- const int signY = y0 < y1 ? 1 : -1;
- int d = deltaX - deltaY;
- gfSetPixel(x1, y1, color);
- while (x0 != x1 || y0 != y1) {
- gfSetPixel(x0, y0, color);
- const int d2 = d << 1;
- if (d2 > -deltaY) { // d > -dy / 2
- d -= deltaY;
- x0 += signX;
- }
- if (d2 < deltaX) {
- d += deltaX;
- y0 += signY;
- }
- }
- }
- void DrawLine(TPoint a, TPoint b, const RGBPIXEL& color) {
- DrawLine(a.x, a.y, b.x, b.y, color);
- }
- inline bool cw(const TPoint& a, const TPoint& b, const TPoint& c) { // clockwise
- return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) < 0;
- }
- int sign(long long a) {
- if (a > 0) return 1;
- if (a < 0) return -1;
- return 0;
- }
- inline int SignArea(const TPoint& a, const TPoint& b, const TPoint& c) {
- return sign(static_cast<long long>(b.x - a.x) * (c.y - a.y) - static_cast<long long>(b.y - a.y) * (c.x - a.x));
- }
- inline int Area(const TPoint& a, const TPoint& b) {
- return a.x * b.y - a.y * b.x;
- }
- inline bool Intersect_1(int a, int b, int c, int d) {
- if (a > b) std::swap(a, b);
- if (c > d) std::swap(c, d);
- return max(a, c) <= min(b, d);
- }
- bool Intersect(const TPoint& a, const TPoint& b, const TPoint& c, const TPoint& d) {
- return Intersect_1(a.x, b.x, c.x, d.x)
- && Intersect_1(a.y, b.y, c.y, d.y)
- && SignArea(a, b, c) * SignArea(a, b, d) <= 0
- && SignArea(c, d, a) * SignArea(c, d, b) <= 0;
- }
- inline RGBPIXEL RandColor() {
- return RGBPIXEL(rand() % 256, rand() % 256, rand() % 256);
- }
- bool PointInPolygonWithEvenOddRule(TPolygon& p, const TPoint& a) {
- bool counter = 0;
- const int inf = 2000;
- TPoint b = { a.x, a.y - inf };
- bool pre = false;
- bool cur = false;
- const int n = p.Size();
- for (int i = 0; i < n; i++) {
- cur = Intersect(p[i], p[(i + 1) % n], a, b);
- if (cur && pre && p[i].x == a.x) {
- if ((p[(i - 1 + n) % n].x > a.x) ^ (p[(i + 1) % n].x > a.x)) {
- continue;
- }
- }
- counter ^= cur;
- pre = cur;
- }
- return counter;
- }
- bool PointInPolygonWithNonZeroWindingRule(TPolygon& p, const TPoint& a) {
- int counter = 0;
- const int inf = 9000;
- TPoint b = { a.x, a.y + inf };
- bool pref = false;
- bool cur = false;
- const int n = p.Size();
- for (int i = 0; i < n; i++) {
- if (cur = Intersect(p[i], p[(i + 1) % n], a, b)) {
- if (cur && pref && p[i].x == a.x) {
- if ((p[(i - 1 + n) % n].x - a.x) * (p[(i + 1) % n].x - a.x) < 0) {
- continue;
- }
- }
- counter += SignArea(p[i], p[(i + 1) % n], a);
- }
- pref = cur;
- }
- return counter;
- }
- const TCHAR* GetTextForDraw(const std::string& s) {
- char* answer = new char[s.size() + 1];
- for (int i = 0; i < s.size(); i++) {
- answer[i] = s[i];
- }
- answer[s.size()] = '\0';
- return answer;
- }
- template<typename T>
- const TCHAR* GetTextForDraw(const T& value) {
- return GetTextForDraw(std::to_string(value));
- }
- std::pair<TPoint, TPoint> MinMaxBorders(TPolygon& polygon) {
- const int inf = 1 << 30;
- TPoint minPoint = { inf, inf };
- TPoint maxPoint = { -inf, -inf };
- for (const auto& p : polygon) {
- minPoint.x = min(minPoint.x, p.x);
- minPoint.y = min(minPoint.y, p.y);
- maxPoint.x = max(maxPoint.x, p.x);
- maxPoint.y = max(maxPoint.y, p.y);
- }
- return { minPoint, maxPoint };
- }
- long long DistQrt(const TPoint& a, const TPoint& b) {
- return static_cast<long long>(a.x - b.x) * (a.x - b.x) + static_cast<long long>(a.y - b.y) * (a.y - b.y);
- }
- double C(int n, int m) {
- double answer = 1;
- for (int i = 1; i <= n; i++) {
- answer *= i;
- }
- for (int i = 1; i <= m; i++) {
- answer /= i;
- }
- for (int i = 1; i <= n - m; i++) {
- answer /= i;
- }
- return answer;
- }
- void DrawBezierLine(std::vector<TPoint>& arr, const RGBPIXEL& color) {
- const int n = arr.size();
- if (n <= 2) {
- DrawLine(arr[0], arr.back(), color);
- return;
- }
- auto dist = [&](const TPoint& a) {
- return abs(a.x) + abs(a.y);
- };
- long long D = 0;
- for (int i = 2; i < n; i++) {
- D = max(D, dist(arr[i - 2] - arr[i - 1] - arr[i - 1] + arr[i]));
- }
- double N = 1 + sqrt(n * D);
- double dt = 1 / N;
- auto R = [&](double t) {
- double x = 0;
- double y = 0;
- for (int i = 0; i < n; i++) {
- double p = C(n - 1, i) * pow(t, i) * pow(1 - t, n - 1 - i);
- x += arr[i].x * p;
- y += arr[i].y * p;
- }
- return TPoint(x + 0.5, y + 0.5);
- };
- std::vector<TPoint> a;
- int q = 1 / dt + 1;
- for (int i = 0; i < q; i++) {
- a.push_back(R(i * 1.0 / q));
- }
- a.push_back(arr.back());
- for (int i = 1; i < a.size(); i++) {
- DrawLine(a[i - 1], a[i], color);
- }
- }
- std::vector<TPoint> GetBezierLine(std::vector<TPoint>& arr, const RGBPIXEL& color) {
- const int n = arr.size();
- if (n <= 2) {
- DrawLine(arr[0], arr.back(), color);
- return arr;
- }
- auto dist = [&](const TPoint& a) {
- return abs(a.x) + abs(a.y);
- };
- long long D = 0;
- for (int i = 2; i < n; i++) {
- D = max(D, dist(arr[i - 2] - arr[i - 1] - arr[i - 1] + arr[i]));
- }
- double N = 1 + sqrt(n * D);
- double dt = 1 / N;
- auto R = [&](double t) {
- double x = 0;
- double y = 0;
- for (int i = 0; i < n; i++) {
- double p = C(n - 1, i) * pow(t, i) * pow(1 - t, n - 1 - i);
- x += arr[i].x * p;
- y += arr[i].y * p;
- }
- return TPoint(x + 0.5, y + 0.5);
- };
- std::vector<TPoint> a;
- int q = 1 / dt + 1;
- for (int i = 0; i < q; i++) {
- a.push_back(R(i * 1.0 / q));
- }
- a.push_back(arr.back());
- return a;
- }
- /*
- de Kasteljo
- */
- void DrawBezierLine2(std::vector<TPoint>& arr, const RGBPIXEL& color) {
- const int n = arr.size();
- if (n <= 2) {
- DrawLine(arr[0], arr.back(), color);
- return;
- }
- const int bits = 7;
- const int size = 1 << bits;
- const int halfsize = 1 << (bits - 1);
- std::function<TPoint(const TPoint&, const TPoint&, int)> get = [&](const TPoint& a, const TPoint& b, int pr) {
- return TPoint(a.x + (pr * (b.x - a.x) + halfsize >> bits), a.y + (pr * (b.y - a.y) + halfsize >> bits));
- };
- std::function<TPoint(std::vector<TPoint>, int)> findPoint = [&](std::vector<TPoint> a, int pr) {
- if (a.size() <= 2) {
- return get(a[0], a.back(), pr);
- }
- else {
- for (int i = 0; i + 1 < a.size(); i++) {
- a[i] = get(a[i], a[i + 1], pr);
- }
- a.pop_back();
- return findPoint(a, pr);
- }
- };
- std::vector<TPoint> result;
- for (int t = 0; t <= size; t++) {
- result.push_back(findPoint(arr, t));
- }
- for (int i = 0; i + 1 < result.size(); i++) {
- DrawLine(result[i], result[i + 1], color);
- }
- }
- int ClipLine(TPoint& a, TPoint& b, TPolygon& arr) {
- double t0 = 0, t1 = 1, t;
- TPoint s = TPoint(b.x - a.x, b.y - a.y); // vector colinear with current
- long long nx, ny, num, denom, x1, y1, x2, y2;
- const int n = arr.Size();
- if (n <= 2 || !arr.IsConvex()) {
- return 1;
- }
- bool isCw = false;
- for (int i = 0; i + 2 < n; i++) {
- isCw |= SignArea(arr[i + 0], arr[i + 1], arr[i + 2]) < 0; // polygon contains edges in clock wise order
- }
- for (int i = (isCw ? 0 : n - 1); (isCw ? i < n : i >= 0); (isCw ? i++ : i--)) {
- nx = arr[i + (isCw ? 1 : -1)].y - arr[i].y;
- ny = arr[i].x - arr[i + (isCw ? 1 : -1)].x;
- denom = nx * s.x + ny * s.y;
- num = nx * (a.x - arr[i].x) + ny * (a.y - arr[i].y);
- if (abs(denom) > 1e-9) {
- t = -num * 1.0 / denom;
- if (denom > 0) {
- if (t > t0) {
- t0 = t;
- }
- }
- else {
- if (t < t1) {
- t1 = t;
- }
- }
- }
- else {
- if (num < 0) {
- return 1;
- }
- }
- }
- if (t0 <= t1) {
- x1 = a.x + t0 * (b.x - a.x);
- y1 = a.y + t0 * (b.y - a.y);
- x2 = a.x + t1 * (b.x - a.x);
- y2 = a.y + t1 * (b.y - a.y);
- a = TPoint(x1 + 0.5, y1 + 0.5); // update values
- b = TPoint(x2 + 0.5, y2 + 0.5);
- return 0;
- }
- return 1; // no visible part
- }
- bool PointIntoTriangle(TPoint a, TPoint b, TPoint c, TPoint x) {
- return SignArea(a, b, x) * SignArea(a, x, c) <= 0 && SignArea(b, c, x) * SignArea(b, x, a) <= 0 && SignArea(c, a, x) * SignArea(c, x, b) <= 0;
- }
- bool PointIntoConvexPolygon(TPolygon p, TPoint x) {
- bool ans = false;
- const int n = p.Size();
- if (n <= 2) return false;
- for (int i = 2; !ans && i < n; i++) {
- ans |= PointIntoTriangle(p[i - 2], p[i - 1], p[0], x);
- }
- return ans;
- }
- int Y = 15;
- int Dy = 20;
- std::vector<TPolygon> BuildProjectionOfParallelepiped(const std::vector<std::vector<TPoint3D>>& p) {
- const int n = p.size();
- int ITR = 0;
- auto isInvisible = [&](TPoint3D& pt, int index) {
- for (int i = 0; i < n; i++) {
- if (i == index) continue;
- TPolygon cur(p[i].size());
- for (int j = 0; j < p[i].size(); j++) {
- cur[j] = TPoint(p[i][j].x, p[i][j].y);
- }
- if (!PointIntoConvexPolygon(cur, TPoint(pt.x, pt.y))) continue;
- TPoint X = TPoint(pt.x, pt.y) - cur[0];
- TPoint A = cur[1] - cur[0];
- TPoint B = cur[2] - cur[0];
- int dZ1 = p[i][1].z - p[i][0].z;
- int dZ2 = p[i][2].z - p[i][0].z;
- int dZ = pt.z - p[i][0].z;
- double t2 = static_cast<double>(X.x * A.y - A.x * X.y) / (A.y * B.x - A.x * B.y);
- double t1 = static_cast<double>(X.x - B.x * t2) / A.x;
- double curX = t1 * A.x + t2 * B.x;
- double curY = t1 * A.y + t2 * B.y;
- double curZ = dZ1 * t1 + dZ2 * t2 + p[i][0].z;
- auto myCeil = [&](double x) {
- if (x < 0) return static_cast<int>(x - 0.5);
- return static_cast<int>(x + 0.5);
- };
- curX = myCeil(curX);
- curY = myCeil(curY);
- curZ = myCeil(curZ);
- if (curZ < pt.z) {
- return true;
- }
- }
- return false;
- };
- std::vector<TPolygon> res;
- for (int i = 0; i < n; i++) {
- TPoint3D mnPt = p[i][0];
- for (auto& pt : p[i]) {
- if (pt.z < mnPt.z) {
- mnPt = pt;
- }
- }
- if (isInvisible(mnPt, i)) {
- res.emplace_back(p[i].size());
- for (int j = 0; j < p[i].size(); j++) {
- res.back()[j] = TPoint(p[i][j].x, p[i][j].y);
- }
- }
- }
- return res;
- }
- std::vector<bool> BuildProjectionOfParallelepiped2(const std::vector<std::vector<TPoint3D>>& p) {
- std::vector<bool> result(p.size(), true);
- int itr = 0;
- for (const auto& v : p) {
- const int n = v.size();
- bool isInvisible = false;
- for (int i = 0; !isInvisible && i < n; i++) {
- TPoint a{ v[i].x, v[i].y };
- TPoint b{ v[(i + 1) % n].x, v[(i + 1) % n].y };
- TPoint c{ v[(i + 2) % n].x, v[(i + 2) % n].y };
- isInvisible = Area(b - a, c - b) > 0;
- }
- result[itr++] = !isInvisible;
- }
- return result;
- }
- std::vector<bool> BuildPointProjection(std::vector<std::vector<TPoint3D>>& p, int k) {
- for (auto& v : p) {
- for (auto& pt : v) {
- }
- }
- return BuildProjectionOfParallelepiped2(p);
- }
- std::vector<std::vector<TPoint3D>> p;
- double phi = M_PI / 20;
- double x = 6;
- double y = 1;
- double z = 9;
- double angle = 0;
- std::vector<RGBPIXEL> colors = {
- RGBPIXEL::White(),
- RGBPIXEL::Red(),
- RGBPIXEL::Green(),
- RGBPIXEL::Blue(),
- RGBPIXEL::DkMagenta(),
- RGBPIXEL::Yellow(),
- };
- const int WINDOW_H = 900;
- const int WINDOW_W = 1.5 * 900;
- bool gfInitScene() {
- try {
- const int WINDOW_SIZE = 900;
- gfSetWindowSize(WINDOW_W, WINDOW_H);
- const int A = 180;
- const int B = 100;
- const int C = 150;
- int Norm = x * x + y * y + z * z;
- x /= sqrt(Norm);
- y /= sqrt(Norm);
- z /= sqrt(Norm);
- p = {
- { { 0, 0, 0 },{ 0, B, 0 },{ 0, B, C },{ 0, 0, C } },
- { { A, 0, C },{ A, B, C },{ A, B, 0 },{ A, 0, 0 } },
- { { 0, 0, 0 },{ 0, 0, C },{ A, 0, C },{ A, 0, 0 } },
- { { A, B, 0 },{ A, B, C },{ 0, B, C },{ 0, B, 0 } },
- { { A, 0, 0 },{ A, B, 0 },{ 0, B, 0 },{ 0, 0, 0 } },
- { { 0, 0, C },{ 0, B, C },{ A, B, C },{ A, 0, C } }
- };
- for (auto& v : p) {
- for (auto& pt : v) {
- pt.x += 300;
- pt.y += 300;
- }
- }
- double xyAngle = 45.0 / 180.0 * M_PI;
- double xzAngle = 45.0 / 180.0 * M_PI;
- double yzAngle = 0 / 180.0 * M_PI;
- double cosxy = cos(xyAngle);
- double cosxz = cos(xzAngle);
- double cosyz = cos(yzAngle);
- double sinxy = sin(xyAngle);
- double sinxz = sin(xzAngle);
- double sinyz = sin(yzAngle);
- // xy
- for (auto& v : p) {
- for (auto& pt : v) {
- TPoint3D nxt;
- nxt.x = cosxy * pt.x - sinxy * pt.y;
- nxt.y = sinxy * pt.x + cosxy * pt.y;
- nxt.z = pt.z;
- pt = nxt;
- }
- }
- // xz
- for (auto& v : p) {
- for (auto& pt : v) {
- TPoint3D nxt;
- nxt.x = cosxz * pt.x - sinxz * pt.z;
- nxt.z = sinxz * pt.x + cosxz * pt.z;
- nxt.y = pt.y;
- pt = nxt;
- }
- }
- // yz
- for (auto& v : p) {
- for (auto& pt : v) {
- TPoint3D nxt;
- nxt.y = cosyz * pt.y - sinyz * pt.z;
- nxt.z = sinyz * pt.y + cosyz * pt.z;
- nxt.x = pt.x;
- pt = nxt;
- }
- }
- int mnX = 1e9;
- int mnY = 1e9;
- int mnZ = 1e9;
- for (auto& v : p) {
- for (auto& pt : v) {
- mnX = min(mnX, pt.x);
- mnY = min(mnY, pt.y);
- mnZ = min(mnZ, pt.z);
- }
- }
- for (auto& v : p) {
- for (auto& pt : v) {
- pt.x -= mnX - 100;
- pt.y -= mnY - 100;
- pt.z -= mnZ - 100;
- }
- }
- for (auto v : p) {
- int dx = 0;
- for (int i = 0; i < v.size(); i++) {
- DrawLine(TPoint(v[i].x + dx, v[i].y), TPoint(v[(i + 1) % v.size()].x + dx, v[(i + 1) % v.size()].y), RGBPIXEL::Green());
- }
- }
- auto cur = BuildProjectionOfParallelepiped2(p);
- for (int i = 0; i < cur.size(); i++) {
- if (cur[i]) {
- TPolygon x(p[i].size());
- for (int j = 0; j < p[i].size(); j++) {
- x[j] = TPoint(p[i][j].x, p[i][j].y);
- }
- x.Draw(RGBPIXEL::White());
- }
- }
- const int DX = 500;
- auto myWay = BuildProjectionOfParallelepiped(p);
- for (auto& v : myWay) {
- for (auto& pt : v) {
- pt.x += DX;
- }
- v.Draw(RGBPIXEL::Red());
- }
- TPoint3D mxPt = p[0][0];
- for (auto& v : p) {
- for (auto& pt : v) {
- if (pt.z > mxPt.z) {
- mxPt = pt;
- }
- }
- }
- gfDrawRectangle(mxPt.x - 5, mxPt.y - 5, mxPt.x + 5, mxPt.y + 5, RGBPIXEL::Red());
- gfDrawRectangle(mxPt.x - 5 + DX, mxPt.y - 5, mxPt.x + 5 + DX, mxPt.y + 5, RGBPIXEL::Red());
- }
- catch (...) {
- return false;
- }
- return true;
- }
- int dx = 10;
- int dy = 15;
- int sx = 0;
- int sy = 0;
- int ITR = 0;
- void gfDrawScene()
- {
- gfClearScreen(RGBPIXEL::Black());
- angle += phi;
- double s = sin(angle);
- double c = cos(angle);
- const int Y1 = 50;
- const int X1 = 100;
- ITR++;
- ITR %= 500;
- std::string str1 = "спокойствие...";
- std::string str2 = "блаженство...";
- if (ITR < 250) {
- gfDrawText(X1, Y1, GetTextForDraw(str1), RGBPIXEL::White());
- }
- else {
- gfDrawText(X1 + 50, Y1 + 50, GetTextForDraw(str2), RGBPIXEL::White());
- }
- std::vector<std::vector<TPoint3D>> cur;
- for (auto& v : p) {
- cur.emplace_back();
- for (auto& pt : v) {
- TPoint3D nxt;
- double cx = (c + (1 - c) * x * x) * pt.x + ((1 - c) * x * y - s * z) * pt.y + ((1 - c) * x * z + s * y) * pt.z;
- double cy = ((1 - c) * y * x + s * z) * pt.x + (c + (1 - c) * y * y) * pt.y + ((1 - c) * y * z - s * x) * pt.z;
- double cz = ((1 - c) * z * x - s * y) * pt.x + ((1 - c) * z * y + s * x) * pt.y + (c + (1 - c) * z * z) * pt.z;
- int k = 100; //with point projection
- double H = cz / k + 1;
- nxt.x = cx / H;
- nxt.y = cy / H;
- nxt.z = k / H;
- /*nxt.x = cx; //without point projection
- nxt.y = cy;
- nxt.z = cz; */
- cur.back().push_back(nxt);
- }
- }
- auto kek = BuildProjectionOfParallelepiped2(cur);
- for (int i = 0; i < kek.size(); i++) {
- if (kek[i]) {
- TPolygon curp(cur[i].size());
- for (int j = 0; j < curp.Size(); j++) {
- curp[j] = TPoint(cur[i][j].x + 500, cur[i][j].y + 500);
- }
- curp.Draw(RGBPIXEL::White());
- }
- }
- //
- }
- void gfCleanupScene() {
- }
- void gfOnLMouseClick(int x, int y) {
- }
- void gfOnRMouseClick(int x, int y) {
- }
- void gfOnKeyDown(UINT key) {
- }
- void gfOnKeyUp(UINT key)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement