Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <math.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include "glut.h"
- class Vector2
- {
- public:
- int x;
- int y;
- Vector2() { }
- Vector2(int _x, int _y)
- {
- x = _x;
- y = _y;
- }
- };
- Vector2 CollisionPoint(Vector2 a, Vector2 b, Vector2 c, Vector2 d)
- {
- if (((a.y - b.y)*(d.x - c.x) - (c.y - d.y)*(b.x - a.x)) == 0)
- {
- return Vector2(0, 0);
- }
- if (d.y > a.y)
- {
- return Vector2(0, 0);
- }
- Vector2 T;
- T.x = -((a.x*b.y - b.x*a.y)*(d.x - c.x) - (c.x*d.y - d.x*c.y)*(b.x - a.x)) / ((a.y - b.y)*(d.x - c.x) - (c.y - d.y)*(b.x - a.x));
- if ((d.x - c.x) == 0)
- {
- T.y = a.y;
- }
- else
- {
- T.y = ((c.y - d.y)*(-T.x) - (c.x*d.y - d.x*c.y)) / (d.x - c.x);
- }
- return T;
- }
- int width = 390, height = 320;
- void PutPixel(int x, int y, int* Color) {
- glBegin(GL_POINTS);
- glColor3ub(Color[0], Color[1], Color[2]);
- glVertex2i(x, y);
- glEnd();
- }
- void DrawLine(int x1, int y1, int x2, int y2, int* Color) {
- int dx, dy, i, e;
- int incx, incy, inc1, inc2;
- int x, y;
- dx = x2 - x1;
- dy = y2 - y1;
- if (dx < 0) dx = -dx;
- if (dy < 0) dy = -dy;
- incx = 1;
- if (x2 < x1) incx = -1;
- incy = 1;
- if (y2 < y1) incy = -1;
- x = x1; y = y1;
- if (dx > dy) {
- PutPixel(x, y, Color);
- e = 2 * dy - dx;
- inc1 = 2 * (dy - dx);
- inc2 = 2 * dy;
- for (i = 0; i<dx; i++) {
- if (e >= 0) {
- y += incy;
- e += inc1;
- }
- else
- e += inc2;
- x += incx;
- PutPixel(x, y, Color);
- }
- }
- else {
- PutPixel(x, y, Color);
- e = 2 * dx - dy;
- inc1 = 2 * (dx - dy);
- inc2 = 2 * dx;
- for (i = 0; i<dy; i++) {
- if (e >= 0) {
- x += incx;
- e += inc1;
- }
- else
- e += inc2;
- y += incy;
- PutPixel(x, y, Color);
- }
- }
- }
- int Color[3] = { 239,202,193 };
- int BorderColor[3] = { 209,172,163 };
- void Display(void){
- glClear(GL_COLOR_BUFFER_BIT);
- std::vector<Vector2> points;
- points.push_back(Vector2(100, 100));
- points.push_back(Vector2(100, 120));
- points.push_back(Vector2(80, 120));
- points.push_back(Vector2(80, 200));
- points.push_back(Vector2(100, 200));
- points.push_back(Vector2(100, 220));
- points.push_back(Vector2(280, 220));
- points.push_back(Vector2(280, 200));
- points.push_back(Vector2(300, 200));
- points.push_back(Vector2(300, 120));
- points.push_back(Vector2(280, 120));
- points.push_back(Vector2(280, 100));
- points.push_back(Vector2(100, 100));
- for (int i = 0; i < points.size() - 1; i++)
- {
- DrawLine(points[i].x, points[i].y, points[i + 1].x, points[i + 1].y, BorderColor);
- }
- //1.0 min && max
- int ymin = INT_MAX;
- int ymax = INT_MIN;
- int xmin = INT_MAX;
- int xmax = INT_MIN;
- for (int i = 0; i < points.size() - 1; i++)
- {
- if (points[i].y < ymin)
- {
- ymin = points[i].y;
- }
- if (points[i].y > ymax)
- {
- ymax = points[i].y;
- }
- if (points[i].x < xmin)
- {
- xmin = points[i].x;
- }
- if (points[i].x > xmax)
- {
- xmax = points[i].x;
- }
- }
- Vector2 collision;
- Vector2 sourcePoint;
- for (int y = ymin + 1; y < ymax; y++)
- {
- for (int i = 0; i < points.size() - 1; i++)
- {
- if (points[i].y >= y)
- {
- continue;
- }
- if (points[i + 1].x > points[i].x)
- {
- continue;
- }
- for (int j = i; j < points.size() - 1; j++)
- {
- sourcePoint = CollisionPoint(Vector2(-1000, y), Vector2(1000, y), points[i+1], points[i]);
- if (sourcePoint.x == 0 && sourcePoint.y == 0 || sourcePoint.x > xmax || sourcePoint.x < xmin || sourcePoint.x < points[i].x)
- {
- continue;
- }
- collision = CollisionPoint(sourcePoint, Vector2(sourcePoint.x + 1000, y), points[j], points[j + 1]);
- if (collision.x == 0 && collision.y == 0
- || collision.x > xmax || collision.x < xmin
- || collision.x < points[i].x)
- {
- continue;
- }
- /*if ((collision.x == 0 && collision.y == 0
- || !(collision.x > xmax || collision.x < xmin
- || collision.x < points[i].x)))
- {
- continue;
- }*/
- DrawLine(points[i].x + 1, y, collision.x - 1, y, Color);
- }
- }
- glFlush();
- }
- }
- void Display2(void){
- glClear(GL_COLOR_BUFFER_BIT);
- std::vector<Vector2> points;
- points.push_back(Vector2(100, 100));
- points.push_back(Vector2(100, 120));
- points.push_back(Vector2(80, 120));
- points.push_back(Vector2(80, 200));
- points.push_back(Vector2(100, 200));
- points.push_back(Vector2(100, 220));
- points.push_back(Vector2(280, 220));
- points.push_back(Vector2(280, 200));
- points.push_back(Vector2(300, 200));
- points.push_back(Vector2(300, 120));
- points.push_back(Vector2(280, 120));
- points.push_back(Vector2(280, 100));
- points.push_back(Vector2(100, 100));
- for (int i = 0; i < points.size() - 1; i++)
- {
- DrawLine(points[i].x, points[i].y, points[i + 1].x, points[i + 1].y, BorderColor);
- }
- //1.0 min && max
- int ymin = INT_MAX;
- int ymax = INT_MIN;
- for (int i = 0; i < points.size(); i++)
- {
- if (points[i].y < ymin)
- ymin = points[i].y;
- if (points[i].y > ymax)
- ymax = points[i].y;
- }
- //2.0 for ymin->ymax
- for (int y = ymin+1; y < ymax; y+=1)
- {
- //2.1 calc
- std::vector<int> xk;
- for (int i = 0; i < points.size() - 3; i++)
- {
- int xn = points[i].x;
- int one = (points[i + 1].x - points[i].x);
- int two = (y - points[i].y);
- int dividend = abs(one*two);
- int divider = points[i + 1].y - points[i].y;
- if (divider != 0)
- {
- xk.push_back(xn + (dividend / divider));
- }
- }
- //2.2 sort
- std::sort(xk.begin(), xk.end());
- //2.3 draw
- for (int i = 0; i < xk.size()-1; i++)
- {
- DrawLine(xk[i], y, xk[i + 1], y, Color);
- }
- }
- glFlush();
- }
- void Init(){
- glClearColor(1.0f, 0.941f, 0.913f, 1.0f);
- gluOrtho2D(0, width, 0, height);
- }
- void main(int argc, char **argv){
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowPosition(0, 0);
- glutInitWindowSize(width, height);
- glutCreateWindow("SRS#3 Fill");
- Init();
- glutDisplayFunc(Display);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment