PDA

View Full Version : Find Point on rect edge based on angle



bobalouie
September 19th, 2007, 04:33 AM
I did not find anything that helped me with this anywhere on the net, and I racked my brain for what seemed like...
Well... just too long to figure it out. I'm not a math guru so I had revisted my old homework assignments to finally get it.
But I thought I would post it here to help anyone who might want it.
The example is done with a square, but it could easily be modified for a rectagle since I had to use quadrants anyway. The hardest thing was working with the degrees, because of flash's geometry system is so wonkey when it comes to degrees. But anywho...

Check out the example here (you may drag the squares):
http://www.jimisaacs.com/test/geomTest3/

The fla for it is here:
http://www.jimisaacs.com/test/geomTest3/geomTest3.zip

toodles

Intravenus
January 6th, 2008, 02:29 PM
Hey there. I'd like to have a look at this file but when I tried to open it I get an Unexpected File format error in both Flash 8 and Flash MX...

any suggestions?

Templarian
January 6th, 2008, 03:34 PM
^Probably Flash CS3

Intravenus
January 6th, 2008, 04:02 PM
Can someone with CS3 save this as Flash 8 format or doesn't it work like that?

ostmalm
January 6th, 2008, 05:37 PM
here you go

Intravenus
January 6th, 2008, 07:29 PM
Excellent! Thank you.

Sirisian
January 6th, 2008, 08:18 PM
There is no trig involved in this. It only takes 1 function. You should have just asked for the solution I would have posted it.

Concept. You have a line from the center of one rectangle to another. Each rectangle has 4 sides, so a simple line to line intersection test is used. :)



public function LineToLineIntersection(x1_:Number, y1_:Number, x2_:Number, y2_:Number, x3_:Number, y3_:Number, x4_:Number, y4_:Number):Object{
var result:Object = {b:false, x:0, y:0};
var r:Number, s:Number, d:Number;
d = (((x2_-x1_)*(y4_-y3_))-(y2_-y1_)*(x4_-x3_));
if(d != 0){
r = (((y1_-y3_)*(x4_-x3_))-(x1_-x3_)*(y4_-y3_))/d;
s = (((y1_-y3_)*(x2_-x1_))-(x1_-x3_)*(y2_-y1_))/d;
if (r >=0 && r <= 1){
if (s >= 0 && s <= 1){
result.b = true;
result.x = x1_ + r * (x2_ - x1_);
result.y = y1_ + r * (y2_ - y1_);
}
}
}
return result;
}
simple to use.

var i:object = LineToLineIntersection(...);
Line 1 is the first 4 number aka 2 points. The next line is the
if(i.b){
//intersection
i.x and i.y are the intersection points
}

optimizations can be used and such, but I think you get the idea.

Good luck :)

What was this for?

Intravenus
January 7th, 2008, 09:26 AM
I actually only needed a small part of this code and I have a feeling that there is a tidier way of doing what I want.

Basically I want to find the X and Y of the point where a line drawn from the center of a square intersects the border of the square.

I know the size of the square and the angle of the line drawn.

I managed it with IF loops checking the angle of the drawn line but I can't shake the feeling that there is a much simpler way of achieving this.

Any ideas?

Sirisian
January 7th, 2008, 07:20 PM
You can use the intersection algorithm I posted.

x1 = center.x
y1 = center.y
x2 = center.x + Math.cos(angle) * squareWidth;
y2 = center.y + Math.sin(angle) * squareWidth;
(x3,y3) and (x4,y4) are just 2 vertices forming one edge. Iterate through all 4 edges and perform the function and one of them will have b in the returned object equal to true. In that case the x and y in the object will be where it intersects.

You can actually use a ray versus line check too.

aiMarz
January 13th, 2008, 11:27 AM
One easy optimization technique that I use to use in older DOS programming days when cos and sin where boggling functions was to store the individual values of sin and cos up to 360 degrees in an array and then use these. The sin and cos functions used by programming api's and math functions use up more computer processes than using a simple array to store everything in. This is just a very simple optimization technique, like stated, that will help increase the performance of your program if you plan on using a lot of tests with this in it.