MH
MH
Originally posted by Ulf Dittmer:
To minimally improve efficiency you can omit the call to Math.sqrt, since that is applied to both sides of the equality comparison.
Also, instead of casting doubles to ints, I'd do something like "if Math.abs(diglen1 - diglen2) < EPSILON" with some appropriate bound for EPSILON. Not for efficiency, but for correctness.
A problem with this approach is that there are shapes for which "true" will be returned even though the figure is not a rectangle, e.g. (0,0) (1,1) (2,0) (1,3).
Originally posted by Arjun Shastry:
If sides of rectangle are not parallel to X/Y axes then we can check for product of slopes of adjacent sides.m1*m2 must be -1 if sides are perpendicular. and then we can check if opposite pair is of same length.
If sides of rectangle are parallel to XY then no need to check for slope products.Will this be efficient? not sure
[ October 29, 2007: Message edited by: Arjun Shastry ]
Also, instead of casting doubles to ints, I'd do something like "if Math.abs(diglen1 - diglen2) < EPSILON" with some appropriate bound for EPSILON. Not for efficiency, but for correctness.
It's not clear.
I'm not sure you really need to check the length of the sides at all. I thing you can do it by A) checking the slopes of opposite sides are equal (again, use the difference < Epsilon technique), and B) that the product of the two slope values is about -1 (there's that pesky Epsilon again). if you have a parallelagram (A), and that the sides meet at right angles (B), you have a rectangle.Any other way to check for rectangle?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
"I'm not back." - Bill Harding, Twister
Originally posted by Arjun Shastry:
If sides of rectangle are not parallel to X/Y axes then we can check for product of slopes of adjacent sides.m1*m2 must be -1 if sides are perpendicular. and then we can check if opposite pair is of same length.
If sides of rectangle are parallel to XY then no need to check for slope products.Will this be efficient? not sure
[ October 29, 2007: Message edited by: Arjun Shastry ]
Originally posted by ankur rathi:
It Seems to be working.
[ October 31, 2007: Message edited by: Ulf Dittmer ]
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
Those first two conditions ( (ax==bx && by==cy) and (ay==by && bx==cx) ) are not required at all. They're completely covered by the third condition.
But the following both report true, even though they're obviously not rectangles:
...Four superimposed points:
...Two superimposed lines:
"I'm not back." - Bill Harding, Twister
Well, they do. In mathematics the number zero is just as good as the numbers greater than zero. This rule applies to all mathematical concepts starting with the empty set and going on from there, and it always applies unless there are reasons not to apply it. For example it's true that x/x = 1 for non-zero x, but there are reasons why you can't extend that to the case where x is zero. But for the rectangle, there aren't any such reasons, so if you can have a square of side x for any positive x then you can have a square of side zero too.Originally posted by Ryan McGuire:
Then again, I could argue that four superimposed points DO make a rectangle.]
"I'm not back." - Bill Harding, Twister
Two non-zero vectors a and b are perpendicular if and only if a � b = 0.
In particular, two vectors are considered orthogonal if their dot product is zero
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |