Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<Point> Core::clipedge(vector<Point> polygon, int x0, int y0, int x1, int y1){
- std::vector<Point>temp;
- temp.push_back(Point(0,0));
- temp.push_back(Point(0,0));
- temp.push_back(Point(0,0));
- std::vector<Point> re;
- re=temp;
- return re;
- }
- class Point{
- public:
- int x;
- int y;
- Uint8 r;
- Uint8 g;
- Uint8 b;
- Point(int x, int y, Uint8 r = 255, Uint8 g =255, Uint8 b =255) : x(x), y(y), r(r), g(g), b(b) {}
- };
- /////////////////////////////////////
- // Clips the triangle against the screen boundary.
- std::vector<Point> Core::clip(std::vector<Point> polygon)
- {
- // We can only work with at least a triangle.
- if (polygon.size() < 3)
- return polygon;
- // The points p0(x0,y0) and p1(x1,y1) are ordered in clipedge such that any points
- // to the right of the line p0-p1 are inside and vice versa
- polygon = clipedge(polygon, 0, 0, width-1, 0); // Top edge
- polygon = clipedge(polygon, 0, height-1, 0, 0); // Left edge
- polygon = clipedge(polygon, width-1, height-1, 0, height-1); // Bottom edge
- polygon = clipedge(polygon, width-1, 0, width-1, height-1); // Right edge
- return polygon;
- }
- ///////////////////////////////////////////////////////////
- // Clips for any line defined by P0(x0, y0), P1(x1, y1).
- vector<Point> Core::clipedge(vector<Point> polygon, int x0, int y0, int x1, int y1)
- {
- //color random for this stage
- int r=rand()%255;
- int g=rand()%255;
- int b=rand()%255;
- // For each side of the polygon, check the 4 cases of sutherland-hodgman.
- // Creating a temporary buffer to hold your new set of vertices for the clipped polygon.
- vector<Point>temp;
- temp.push_back(Point(1,1));
- int size = (int)polygon.size();
- Point p1 (0, 0);
- for (int i = 0; i < size; ++i) {
- // Points we're testing
- Point p0 = polygon[i];
- p1 = polygon[(i+1)%size]; // The last point is the same as the first point
- // Testing sides
- // Assumes the right hand side of the line being inside.
- int p0_side = testing_side(p0.x, p0.y, x0, y0, x1, y1);
- int p1_side = testing_side(p1.x, p1.y, x0, y0, x1, y1);
- if (x0-x1==0){
- if (p0_side<0){
- if (p1_side<0){
- //case 1 two point out igone
- }
- else if(p1_side>=0){
- //case 2 out-to-in interception pt and end pt
- //line equaltion
- //line equaltion
- int k=(p1.y-p0.y)/(p1.x-p0.x);
- int nb=p0.y-(k*p0.x);
- int ny=(int)(k*x0)+nb;
- temp.push_back(Point(x0,ny,r,g,b));
- temp.push_back(p1);
- }
- }else if(p0_side>=0){
- if (p1_side<0){
- //case 3 in-to-out interception pt
- //line equaltion
- int k=(y0-y1)*(x1-x0);
- k=k+(y1*x0)-(y0*x0);
- int nx=(int)k/(y1-y0);
- temp.push_back(Point(nx,y0,r,g,b));
- }
- else if (p1_side>=0){
- //case 4 in-in end pt
- temp.push_back(p1);
- }
- }
- }
- else if(y0-y1==0){
- if (p0_side<0){
- if (p1_side<0){
- //case 1 two point out igone
- }
- else if(p1_side>=0){
- //case 2 out-to-in interception pt and end pt
- //line equaltion
- int k=(y0-y1)*(x1-x0);
- k=k+(y1*x0)-(y0*x0);
- int nx=(int)k/(y1-y0);
- temp.push_back(Point(nx,y0,r,g,b));
- temp.push_back(p1);
- }
- }else if(p0_side>=0){
- if (p1_side<0){
- //case 3 in-to-out interception pt
- //line equaltion
- int k=(p1.y-p0.y)/(p1.x-p0.x);
- int nb=p0.y-(k*p0.x);
- int nx=(int)(y0-nb)/k;
- temp.push_back(Point(nx,y0,r,g,b));
- }
- else if (p1_side>=0){
- //case 4 in-in end pt
- temp.push_back(p1);
- }
- }
- }
- //get interception pt
- else{
- //equaltion of line for the intersection point
- int k1=(y1 - y0) / (x1 - x0);
- int b1=y0 - k1*x0;
- int k2=(p1.y-p0.y)/(p1.x-p0.x);
- int b2=p0.y - k2*p0.x;
- if(k1==k2){
- k2=k1+1;
- }
- int in_x=(int)(b2-b1)/(k2-k1);
- int in_y=(int)k1*in_x+b1;
- // Consider the 4 cases of SutherlandñHodgman clipping here.
- if (p0_side<0){
- if (p1_side<0){
- //case 1 two point out igone
- }
- else if(p1_side>=0){
- //case 2 out-to-in interception pt and end pt
- temp.push_back(Point(in_x,in_y,r,g,b));
- temp.push_back(p1);
- }
- }else if(p0_side>=0){
- if (p1_side<0){
- //case 3 in-to-out interception pt
- temp.push_back(Point(in_x,in_y,r,g,b));
- }
- else if (p1_side>=0){
- //case 4 in-in end pt
- temp.push_back(p1);
- }
- }
- // Remember to calculate the color for any intersection points
- //not done yet
- }
- }
- return temp;
- }
- /////////////////////////////////////
- // Tests if the point (x, y) lies on the inside or outside of the line P0(x0, y0),
- // P1(x1, y1). Positive: inside. Negative: outside. Zero: on the line.
- int Core::testing_side(int x, int y, int x0, int y0, int x1, int y1)
- {
- //// Your code here
- //for vertical line
- if (x1-x0==0){
- if(x<x1){
- return -1;
- }
- else if(x==x1){
- return 0;
- }
- else{
- return 1;
- }
- }
- else if (y1-y0==0){
- // for horizontal line
- if (y<y1){
- return -1;
- }else if (y==0){
- return 0;
- }
- else{
- return 1;
- }
- }
- else{
- float k=(y1 - y0) / (x1 - x0);
- float b=y1 - k*x1;
- float spot=(x*k) +b;
- if (spot==y){
- return 0;
- }else if (spot<y) {
- return -1;
- }else{
- return 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement