Friday, July 31, 2009

C++ code ( 4 points to know if its a rectangle or not)?

Given 4 points, how will u figure out if its a rectangle or not?

C++ code ( 4 points to know if its a rectangle or not)?
The previous answers suggested by people before me are completely wrong. This is how you would do it:





1) Consider an Array A of 4 points.





2) Take the first point from the array A[0]





3) Compute the slopes between these points A[0],A[1] and A[0], A[2] and A[0],A[3] . Call these slopes m1, m2 and m3








4) Now If( m1*m2)==-1 then


{





if( A[3],A[1] *A[3], A[2]==-1)


cout %26lt;%26lt; "it is a rectangle/square";


else


cout %26lt;%26lt; "Sorry";


}





5) Note, you also have to similarly check for the other 2 cases. And you are done!





6) All the 3 cases fail, then it is not a rectangle/square
Reply:if the x is the same on vertice(point) pair 1,2 and pair 3,4 while the y of vertices 1,3 and 2,4 are the same then you have a rectangle.
Reply:find the slopes and distances.
Reply:if Point has coordinates x and y,


and rectangle(Point a, Point b, Point c, Point d) is the method


then it's a rectangle if (a.x==b.x %26amp;%26amp; b.y=c.y %26amp;%26amp; c.x==d.x %26amp;%26amp; d.y==a.y)
Reply:Check to see if there are two pairs of the same x coordinates as well as two pairs of the same y coordinates.





bool rectangle(x1, y1, x2, y2, x3, y3, x4, y4)


{


return (x1==x2 %26amp;%26amp; x3 == x4 %26amp;%26amp; y1 == y3 %26amp;%26amp; y2 == y4);


}


No comments:

Post a Comment