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>
- #define all(x) (x).begin(),(x).end()
- #ifndef M_PI
- const double M_PI = 3.1415926535897932384626433832795;
- #endif
- 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);
- }
- private:
- };
- using TPolygon = std::vector<TPoint>;
- inline 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;
- }
- }
- }
- inline void DrawLine(TPoint a, TPoint b, const RGBPIXEL& color) {
- DrawLine(a.x, a.y, b.x, b.y, color);
- }
- void DrawWithRule(TPoint minPoint, TPoint maxPoint, const RGBPIXEL& color, const std::function<bool(TPoint)>& func) {
- for (int x = minPoint.x; x <= maxPoint.x; x++) {
- for (int y = minPoint.y; y <= maxPoint.y; y++) {
- if (func({x, y})) {
- gfSetPixelNoCheck(x, y, color);
- }
- }
- }
- }
- void DrawPolygon(const TPolygon& polygon, const RGBPIXEL& color) {
- int n = polygon.size();
- for (int i = 0; i < n; i++) {
- DrawLine(polygon[i].x, polygon[i].y, polygon[(i + 1) % n].x, polygon[(i + 1) % n].y, color);
- }
- }
- TPolygon GetRandomPolygon(int n, const int& WINDOW_SIZE) {
- TPolygon result(n);
- for (int i = 0; i < n; i++) {
- result[i].x = rand() % WINDOW_SIZE;
- result[i].y = rand() % WINDOW_SIZE;
- }
- return result;
- }
- 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;
- }
- inline bool ccw (const TPoint& a, const TPoint& b, const TPoint& c) { // counter clockwise
- return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) > 0;
- }
- inline int Area (const TPoint& a, const TPoint& b, const TPoint& c) {
- return (b.x - a.x) * (c.y - a.y) - (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)
- && Area(a,b,c) * 1ll * Area(a,b,d) <= 0
- && Area(c,d,a) * 1ll * Area(c,d,b) <= 0;
- }
- inline bool ContainsInTriangle(const TPoint& a, const TPoint& b, const TPoint& c, const TPoint& x) {
- return max(max(cw(b, a, x), cw(x, a, c)), max(cw(a, c, x), max(cw(x, c, b), max(cw(c, b, x), cw(x, b, a))))) <= 0;
- }
- inline bool PointInConvexPolygon(const TPolygon& p, TPoint x) {
- for (int i = 1; i + 1 < p.size(); i++) {
- if (ContainsInTriangle(p[0], p[i], p[i + 1], x)) {
- return true;
- }
- }
- return false;
- }
- inline RGBPIXEL RandColor() {
- return RGBPIXEL(rand() % 256, rand() % 256, rand() % 256);
- }
- bool PointInPolygonWithEvenOddRule(const TPolygon& p, const TPoint& a, const int WINDOW_SIZE) {
- char counter = 0;
- TPoint b = {a.x, a.y - WINDOW_SIZE};
- 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(const TPolygon& p, const TPoint& a, const int WINDOW_SIZE) {
- int counter = 0;
- TPoint b = {a.x, a.y - WINDOW_SIZE};
- 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)) {
- continue;
- }
- }
- if (Area(p[i], p[(i + 1) % n], a) >= 0) {
- counter++;
- } else {
- counter--;
- }
- }
- 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));
- }
- bool IsComplex(const TPolygon& polygon) {
- const int n = polygon.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(polygon[i], polygon[(i + 1) % n], polygon[j], polygon[(j + 1) % n])) {
- return true;
- }
- }
- }
- return false;
- }
- bool IsConvex(const TPolygon& polygon) {
- const int n = polygon.size();
- if (n <= 3) {
- return true;
- }
- if (IsComplex(polygon)) {
- return false;
- }
- double pos = 0;
- double neg = 0;
- for (int i = 0; i < n; i++) {
- const double s = Area(polygon[(i - 1 + n) % n], polygon[i], polygon[(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;
- }
- const char CONVEX = 1;
- const char COMPLEX = 2;
- const char NONCONVEX = 3;
- char Classify(const TPolygon& polygon) {
- const int n = polygon.size();
- if (IsConvex(polygon)) {
- return CONVEX;
- } else if (IsComplex(polygon)) {
- return COMPLEX;
- } else {
- return NONCONVEX;
- }
- }
- std::pair<TPoint, TPoint> MinMaxBorders(const 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};
- }
- bool gfInitScene() {
- try {
- const int WINDOW_SIZE = 600;
- gfSetWindowSize(WINDOW_SIZE, WINDOW_SIZE);
- auto p = GetRandomPolygon(10000, WINDOW_SIZE - 1);
- DrawPolygon(p, RGBPIXEL::White());
- } catch (...) {
- return false;
- }
- return true;
- }
- void gfDrawScene()
- {
- //gfClearScreen(RGBPIXEL::Black());
- //int x = gfGetMouseX(), y = gfGetMouseY();
- //gfDrawRectangle(x - 10, y - 10, x + 10, y + 10, RGBPIXEL::Green());
- }
- 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