Xerain
Xerain

Reputation: 21

Finding the point where a line crosses a nonlinear path

Diagram: https://i.sstatic.net/gpL0P.png

Summary: How do I find the coordinates of the blue point in the diagram so I can calculate the positions of the red points adjacent to it and draw my triangle?

Details of problem: I am trying to make a tail on a drag-able tool-tip balloon for a map application I am making. I would like the the tail to be drawn from the location where the balloon is placed (point 0,0 of the sprite containing both balloon and tail) to the edge of the balloon, with the base of the tail always being the same arbitrary width width it connects to the balloon. The tail would be redrawn at the new coordinates every frame.

I would prefer not to hide part of the tail underneath the balloon, as I intend to use some transparency on it.

If the path were a circle or ellipse, I could use the angleToProgress method (along with some trig) of the CirclePath2D class of greensock's motion path library I could place a hidden path follower and get it's xy... However I am using a roundedRectangle... So I was thinking a better method would be some kind of collision detection between a line and wherever it crosses a path drawn around the perimeter of the balloon. However, I can't find any collision functions that return the xy of the collision.

So I'm stumped. I'm thinking what I want to do may not be easily doable in AS3 using the built in collision methods. Is there a collision library that will let me do this, or, perhaps preferably, some math that is eluding me? Because this seems like I'm over thinking it and it shouldn't be this hard.

[Note: I can't insert the diagram because I don't have enough reputation.]

Upvotes: 2

Views: 298

Answers (1)

lxop
lxop

Reputation: 8595

What are you going to do if the blue intersection point is on one of the rounded corners? Apart from that, it should be pretty easy to figure out - You can do simple line geometry and bonds checking to see where the line intersects with the four infinite lines that make the box boundary, and then determine which is actually the first intersection from pythagorean line length.

If you do this method, you'll need to check the position of the placement with respect to the balloon edges to skip calculating obviously wrong (and sometimes non-existent) intersections (for example, if the balloon centre is directly west of the placement point, then you should skip calculating the intersections of the vertical lines). In fact, you can use similar logic to skip either 2 or 3 sides, depending on the geometry.

Upvotes: 1

Related Questions