Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

C++


XML
#include <graphics.h>
#include <conio.h>

main()
{
   int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   drawpoly(4, points);

   getch();
   closegraph();
   return 0;
}


How did he calculate those points ?

Thanks.
Posted

1 solution

Well, just looking at the numbers, it looks to be a square, rotated so that it's corners are pointing up/down, left & right.

You can use trig functions (and + - for translation) to rotate an objet about an arbitrary angle, though for this case you've presented, it could be done with simple geometry.

I.e using pythagoras, we know that the distance from one corner to the diagonally oposite one is sqrt(1^2 + 1^2) = sqrt(2) times the side length.

So, if you decide on any one of the points, you can calculate the position of the others.
+) Fix the first point (store to array pos 0,1)
+) The opposite corner is sqrt(2)*sidelength away either vertically or horizontally

+) The remaining points are found by taking 1/2 of this sqrt(2)*sidelength
You can then use this value to offset both up/down & right/left to determine the other 2 points.

..........................................  
..........................................  
..........................................  
..........................................  
...................X......................  
..........................................  
..........................................  
..........................................  
..........................................  
..........................................  
.....Z...........................Y........  
..........................................  
..........................................  
..........................................  
..........................................  
..........................................  
..........................................  
..........................................  
...................W......................  
1. Fix point W
                         [X.x = W.x], [X.y = W.y + sqrt(2)*sideLength]
[Y.x = W.x + sqrt(2)*sideLength*0.5], [Y.y = (W.y + X.y) * 0.5]
    [Z.x = Y.x - sqrt(2)*sideLength], [Z.y = Y.y]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900