
find if 4 points on a plane form a rectangle?
1 Expert Answer

Patrick B. answered 06/16/19
Math and computer tutor/teacher
#include <stdio.h>
#include <math.h>
#define IsPerpendicular(m1,m2) ((m1)*(m2)==(-1))
typedef struct _TPoint
{
double x;
double y;
}*TPoint;
double Distance( TPoint p1, TPoint p2)
{
double x1 = p1->x;
double y1 = p1->y;
double x2 = p2->x;
double y2 = p2->y;
return (
sqrt(
(x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)
)
);
}//Distance
int Slope(TPoint p1, TPoint p2, double * mSlope)
{
double x1 = p1->x;
double y1 = p1->y;
double x2 = p2->x;
double y2 = p2->y;
double D = (x2-x1) ;
int iReturn = 0;
if (D!=0)
{
*mSlope = (y2-y1)/D;
}
else
{
iReturn = -1;
}
return(iReturn);
} //Slope
int InputPoint( char * promptMsg, TPoint P)
{
char defaultPromptMsg[25];
if (promptMsg==NULL)
{
sprintf(defaultPromptMsg,"INPUT POINT COORDINATES\n");
promptMsg = &defaultPromptMsg[0];
}
printf("********************************\n");
printf(promptMsg);
printf("\n********************************\n");
printf(" Please input x coordinate :>");
scanf(" %lf",&P->x);
printf(" Please input y coordinate :>");
scanf(" %lf",&P->y);
printf("\n");
}
int SlopeCheck(int M1, int M2, double m1, double m2)
{
int iReturn=0;
if ((M1>=0)&&(M2>=0)) //both slopes are defined
{
if (m1!=m2)
{
iReturn = -1;
}
}
else //one or both slopes are undefined
{
if ((M1==-1) && (M2==-1))
{
}
else //one slope is undefined and the other is not, so they're not equal
{
iReturn = -1;
}
}
return(iReturn);
}
int CornerCheck(int M1, int M2, double m1, double m2)
{
int iReturn=0;
printf(" CornerCheck M1=%d M2 = %d m1=%12.6lf m2 = %12.6lf \n",M1,M2,m1,m2);
if (( M1>=0) && (M2>=0)) //slopes are both defined
{
printf(" CornerCheck: both slopes are defined \n");
if (IsPerpendicular(m1,m2))
{
printf(" CornerCheck: slopes are negative reciprocals-->perpendicular \n");
}
else //the lines are not perpendicular so it is not a rectangle
{
printf(" CornerCheck: slopes are NOT perpendicular \n");
iReturn = -1;
}
}
else //one of the lines is vertical
{
printf(" One or both slopes undefined \n");
if (
(((M1>=0)&&(m1==0)) || ((M2>=0)&&(m2==0))) //slopes are negative reciprocals: one zero, the other undefined
)
{
printf(" LINES ARE PERPENDICULAR \n");
}
else //lines cannot be perpendicular
{
printf(" LINES ARE NOT PERPENDICULAR \n");
iReturn = -1;
}
} //lines are perpendicular
return(iReturn);
}
Rectangle(TPoint P)
{
int iReturn = 0;
struct _TPoint A = P[0];
struct _TPoint B = P[1];
struct _TPoint C = P[2];
struct _TPoint D = P[3];
double mAB, mBC, mCD, mDA; //slopes
double dAB, dBC, dCD, dDA; //distances
int Mab, Mbc, Mcd, Mda; // slopes are defined
Mab = Slope(&A, &B, &mAB);
Mbc = Slope(&B, &C, &mBC);
Mcd = Slope(&C, &D, &mCD);
Mda = Slope(&D, &A, &mDA);
dAB = Distance(&A,&B);
dBC = Distance(&B,&C);
dCD = Distance(&C,&D);
dDA = Distance(&D,&A);
if ((dAB==dCD) && (dBC==dDA))
{
//opposite sides are the same
printf(" DEBUG: opposite sides are the same \n");
if ((SlopeCheck(Mab,Mcd,mAB,mCD)>=0) && (SlopeCheck(Mbc,Mda,mBC,mDA)>=0))
{
//oppposite sides are parallel
printf(" DEBUG: opposite sides are parallel \n");
//checks that the corners are right angles
if ((CornerCheck(Mab,Mbc,mAB,mBC)>=0) &&
(CornerCheck(Mbc,Mcd,mBC,mCD)>=0) &&
(CornerCheck(Mcd,Mda,mCD,mDA)>=0) &&
(CornerCheck(Mda,Mab,mDA,mAB)>=0)
)
{
}
else // one or more of the corners are not right angles, so it cannot be a rectangle
{
iReturn=-1;
}
}
else //opposite sides are not parallel, so it cannot be a rectangle
{
iReturn = -1;
}
}
else // opposite sides are different, so it cannot be a rectangle
{
iReturn = -1;
}
return(iReturn);
}
int Go()
{
int iReturn;
struct _TPoint P[4];
int iLoop;
printf(" Please input the points in CLOCKWISE order starting with top left.....\n");
printf(" Point A is TOP LEFT \n Point B is TOP RIGHT \n Point C is BOTTOM RIGHT \n Point D is BOTTOM LEFT \n");
for (iLoop=0; iLoop<4; iLoop++)
{
char promptMsg[55];
switch (iLoop)
{
case 0:
{
sprintf(promptMsg,"Please input point A"); break;
}
case 1:
{
sprintf(promptMsg,"Please input point B"); break;
}
case 2:
{
sprintf(promptMsg,"Please input point C"); break;
}
case 3:
{
sprintf(promptMsg,"Please input point D"); break;
}
}
InputPoint(promptMsg,&P[iLoop]);
}
if (Rectangle(P)>=0)
{
printf(" IT IS A RECTANGLE \n");
}
else
{
printf(" IT IS NOT A RECTANGLE \n");
}
return(iReturn);
}
int main()
{
return(Go());
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
I have uploaded the source code in the RESOURCE Section under TOOLKIT06/16/19