accepting the exact product proposal one more step, check if two vectors made by any 3 point points are perpendicular, and then see if x and y correspond to the fourth point.
If you have points [Ax, Ay] [Bx, By] [Cx, Cy] [Dx, Dy]
vector v = BA vector u = CA
V (point) U / | V || and | == cos (theta)
therefore, if (vu == 0) there is a pair of perpendicular lines.
Actually, I don’t know C programming, but here is meta-programming for you: P
if (v==[0,0] || u==[0,0] || u==v || D==A) {not a rectangle, not even a quadrilateral} var dot = (v1*u1 + v2*u2); //computes the "top half" of (vu/|v||u|) if (dot == 0) { //potentially a rectangle if true if (Dy==By && Dx==Cx){ is a rectangle } else if (Dx==Bx && Dy==Cy){ is a rectangle } } else {not a rectangle}
there are no square roots in it, and there is no potential for division by zero. I noticed that people mention these issues in earlier posts, so I thought I was proposing an alternative.
So, computationally, you need four subtractions to get v and u, two multiplications, one addition, and you have to check somewhere between 1 and 7 equalities.
Perhaps I do this, but I vaguely remember reading somewhere that subtractions and multiplications are “faster” calculations. I assume that declaring variables / arrays and setting their values is also pretty fast?
Sorry, I'm completely new to these kinds of things, so I would have liked some feedback from what I just wrote.
Edit: try this based on my comment below:
A = [a1,a2]; B = [b1,b2]; C = [c1,c2]; D = [d1,d2]; u = (b1-a1,b2-a2); v = (c1-a1,c2-a2); if ( u==0 || v==0 || A==D || u==v) {!rectangle} // get the obvious out of the way var dot = u1*v1 + u2*v2; var pgram = [a1+u1+v1,a2+u2+v2] if (dot == 0 && pgram == D) {rectangle} // will be true 50% of the time if rectangle else if (pgram == D) { w = [d1-a1,d2-a2]; if (w1*u1 + w2*u2 == 0) {rectangle} //25% chance else if (w1*v1 + w2*v2 == 0) {rectangle} //25% chance else {!rectangle} } else {!rectangle}