zantafio
zantafio

Reputation: 747

Find crossing lines in drawing engine

i am working on a little multimedia - project in which the user, amongst other things, can draw shapes on a canvas (2d) by connecting points with a line. but i need to prohibit the user from crossing lines.

what i am looking for would be a small algorithm that can find intersecting lines. the project is done in AS3/Flash but i guess the answer would be universal.

anyone can give me a clue?

thanks

Upvotes: 1

Views: 315

Answers (2)

Arnaud
Arnaud

Reputation: 7439

Here is an example in Java but I think you can easily adapt to AS3 :

public static boolean intersects(double ax, double ay, double bx, double by,
        double cx, double cy, double dx, double dy) {
    double denum = ((bx-ax)*(dy-cy)-(by-ay)*(dx-cx));
    if (denum == 0) return false; // parallel segments
    double r = ((ay-cy)*(dx-cx)-(ax-cx)*(dy-cy)) / denum;
    double s = 
            ((ay-cy)*(bx-ax)-(ax-cx)*(by-ay)) / denum;
    return 0<=r && r<=1 && 0<=s && s<=1;
}

It should return true if the segments [AB] and [CD] intersect. You can find reference here

Upvotes: 2

paddotk
paddotk

Reputation: 1440

You can use the methods Point, hitTest, hitTestObject or hitTestPoint. I don't know if Super Chafouin's answer is helpful, but I believe that the code looks quite different in AS3 though.

To explain what they mean, I'll quote Aaron Beall from actionscript.org, giving a nice explanation:

hitTestObject checks the bounding box rectangle of two display objects. It always checks rectangle-vs-rectangle.

hitTestPoint checks a point (x,y) against a display object: - With shapeFlag true it checks point-vs-shape - With shapeFlag false it checks point-vs-rectangle

BitmapData/hitTest checks a bitmap against a point, rectangle, or other bitmapdata. Using a check of bitmapdata-vs-bitmapdata means you can perform alpha channel based shape-vs-shape checks. See here: http://www.mikechambers.com/blog/200...ion-detection/

(Also, not to nitpick but just to make sure you understand, neither hitTestObject or hitTestPoint are "triggered" -- you must manually call them when you want to check something.)

Asking for a whole algorithm is quite something, it depends on your app too.

See for more examples and documentation here, here

I hope this will help!

Upvotes: 0

Related Questions