# Dot crossing a line?

**URL:** <https://forum.kirupa.com/t/dot-crossing-a-line/186673>\
**Category:** Uncategorized\
**Created:** [April 23, 2006, 3:10pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673 "2006-04-23T15:10:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uli](https://avatars.discourse-cdn.com/v4/letter/u/ccd318/32.png) [@Uli](https://forum.kirupa.com/u/Uli)\
**Post date:** [April 23, 2006, 3:10pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/1 "2006-04-23T15:10:14Z")

</div>

Hello,

Given 2 point A(x,y) and B(x,y), is there a way to tell if C(x,y) is crossing the line that A,B do? I mean in between thoses to 2 points…  
TiA 😃

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [April 23, 2006, 3:38pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/2 "2006-04-23T15:38:12Z")

</div>

hitTest?

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 23, 2006, 3:43pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/3 "2006-04-23T15:43:48Z")

</div>

yeah, i would say you could use a “lineto” “onEnterFrame” and then hitTest it.

---

<div class="post-metadata">

**Author:** ![Uli](https://avatars.discourse-cdn.com/v4/letter/u/ccd318/32.png) [@Uli](https://forum.kirupa.com/u/Uli)\
**Post date:** [April 23, 2006, 4:04pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/4 "2006-04-23T16:04:16Z")

</div>

well i was looking for a Mathematical way 🙂

---

<div class="post-metadata">

**Author:** ![devonair](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@devonair](https://forum.kirupa.com/u/devonair)\
**Post date:** [April 23, 2006, 5:10pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/5 "2006-04-23T17:10:01Z")

</div>

I’m no mathmatician, and someone here can probably point out the fallacy of this reasoning, but it seems to me that if the slope between the two points that define your line equals the slope between your test point and one of the two line defining points then that test point must be on the line… Right (I’m asking)?  
If that’s actually true, you can use something like this:  
[AS]import flash.geom.Point;  
//  
// Points A & B are known to define line  
var A:Point = new Point(20, 20);  
var B:Point = new Point(50, 50);  
//  
// Point C is point to test  
var C:Point = new Point(10, 10);  
//  
trace(isPointOnLine(A, B, C));  
//  
// First two points define line - p3 is point in question  
function isPointOnLine(p1:Point, p2:Point, p3:Point):Boolean {  
var m1:Number = getSlope(p1, p2);  
if (m1 == -Infinity || m1 == Infinity) m1 = Math.abs(m1);  
var m2:Number = getSlope(p2, p3);  
if (m2 == -Infinity || m2 == Infinity) m2 = Math.abs(m2);  
return (m1 == m2) ? true : false;  
}  
function getSlope(p1:Point, p2:Point):Number {  
var m:Number = (p2.y - p1.y) / (p2.x - p1.x);  
return m;  
}[/AS]

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 23, 2006, 5:20pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/6 "2006-04-23T17:20:17Z")

</div>

Im not sure about this but… when you look at a generic line equasion (y=ax+b) you can use point A x and y coordinates and put them into that your specific exuation (e.g y=2x+1). If you try it with point A[1;1] then you will see that left and right-hand sides of that formula dont match (= point does not belong that line). If you try point B[1;3] you will see that 3=3, thus it lays on that line.

To see if that point also belong only to the line serment between your two points you can just check with a condition or use line-segment equasion instead.

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 23, 2006, 5:39pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/7 "2006-04-23T17:39:27Z")

</div>

lol I got a super easy solution to this

Let be two points P1, P2 and point P3 which you want to check…

When distance |P1, P3| + |P2, P3| is same as distance |P1, P2| then P3 lays on a line between P1 and P2 :hugegrin:

---

<div class="post-metadata">

**Author:** ![GPP](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@GPP](https://forum.kirupa.com/u/GPP)\
**Post date:** [April 23, 2006, 6:39pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/8 "2006-04-23T18:39:11Z")

</div>

[http://www.senocular.com/flash/source.php?id=0.36](http://www.senocular.com/flash/source.php?id=0.36)

Is that what you are looking for =)

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 23, 2006, 7:01pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/9 "2006-04-23T19:01:45Z")

</div>

Thats not realy it, Crayon. Senoculars example show intersection of two lines and finding a cross point… thats not applicable to this problem (at least I dont see the application here 😉 )

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 23, 2006, 7:01pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/10 "2006-04-23T19:01:53Z")

</div>

devonair’s solution is going to be your best bet, the only modification you need to do is make sure that the line is contained within the two points… you can simply do this…

```auto

import flash.geom.Point;
//
// Points A & B are known to define line
var A:Point = new Point(20, 20);
var B:Point = new Point(50, 50);
//
// Point C is point to test
var C:Point = new Point(10, 10);
var D:Point = new Point(30, 30);
//
trace(isPointOnLine(A, B, C));
trace(isPointOnLine(A, B, D));
//
// First two points define line - p3 is point in question
function isPointOnLine(p1:Point, p2:Point, p3:Point):Boolean {
    var m1:Number = getSlope(p1, p2);
    if (m1 == -Infinity || m1 == Infinity) m1 = Math.abs(m1);
    var m2:Number = getSlope(p2, p3);
    if (m2 == -Infinity || m2 == Infinity) m2 = Math.abs(m2);
	 
	 var greaterXPoint:Point = (p1.x > p2.x) ? p1 : p2;
	 var greaterYPoint:Point = (p1.y > p2.y) ? p1 : p2;
	 var lesserXPoint:Point = (p1.x < p2.x) ? p1 : p2;
	 var lesserYPoint:Point = (p1.y < p2.y) ? p1 : p2;
	 
	 if ( p3.x > greaterXPoint.x || p3.y > greaterYPoint.y || p3.x < lesserXPoint.x || p3.y < lesserYPoint.y )
	 {
		 return false;
	 }
	 
    return (m1 == m2) ? true : false;
}
function getSlope(p1:Point, p2:Point):Number {
    var m:Number = (p2.y - p1.y) / (p2.x - p1.x);
    return m;
}

lineStyle(0, 0x0, 100);
moveTo( A.x, A.y );
lineTo( B.x, B.y );

lineStyle(10, 0x0, 100);
moveTo( C.x, C.y );
lineTo( C.x, C.y+1 );

lineStyle(10, 0x00FF00, 100);
moveTo( D.x, D.y );
lineTo( D.x, D.y+1 );

```

Take Care.  
\_Michael

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 23, 2006, 7:17pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/11 "2006-04-23T19:17:17Z")

</div>

Thats too complicated…

```auto
import flash.geom.Point;
// points forming a line segment
var p1:Point = new Point(0, 0);
var p2:Point = new Point(100, 100);
// point to be checked
var A:Point = new Point(50,50);
var B:Point = new Point(60,30);

trace(isPointOnLine(p1, p2, A)); // true
trace(isPointOnLine(p1, p2, B)); // false

function isPointOnLine(p1:Point, p2:Point, p3:Point):Boolean {
	d1 = Point.distance(p1, p3) + Point.distance(p2, p3);
	d2 = Point.distance(p1, p2) ;
	return (d1 == d2) ? true : false;
}

```

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 23, 2006, 7:26pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/12 "2006-04-23T19:26:33Z")

</div>

@matthew.er - I like it, elegant

I’d benchmark the two and see which runs better, I’m not sure how intense the distance formula is in Actionscript in comparison to the conditionals (which are way less) but the memory allocation might push it just over the distance calls.

I’m not sure which way it would work best, if you benchmark please post results, I am interested. I personally would like to know because though it’s a bit more complicated I’d sacrifice how intuitive it is for how quickly it runs.

Take Care.  
\_Michael

---

<div class="post-metadata">

**Author:** ![devonair](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@devonair](https://forum.kirupa.com/u/devonair)\
**Post date:** [April 23, 2006, 7:33pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/13 "2006-04-23T19:33:18Z")

</div>

You’re a super genius… how simple…

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 23, 2006, 7:46pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/14 "2006-04-23T19:46:55Z")

</div>

> [@MichaelxxOA](#):
>
> @matthew.er - I like it, elegant
> 
> I’d benchmark the two and see which runs better, I’m not sure how intense the distance formula is in Actionscript in comparison to the conditionals (which are way less) but the memory allocation might push it just over the distance calls.
> 
> I’m not sure which way it would work best, if you benchmark please post results, I am interested. I personally would like to know because though it’s a bit more complicated I’d sacrifice how intuitive it is for how quickly it runs.
> 
> Take Care.  
> \_Michael

I tested with 1000 calls and my function was about 4 time slower (970ms vs. 270ms). Strange, I always tought that built-in functions are faster 🙂

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 23, 2006, 7:55pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/15 "2006-04-23T19:55:32Z")

</div>

Well the problem is that you are making distance calls…

distance is…

```auto
Math.sqrt( (a * a) + ( b * b ) );

```

The implementation of sqrt is rather intense in comparison to other methods, whether or not it is a built-in function isn’t relative, the implementations are the same.

I hope this make sense, if you have any questions about any of this feel free to ask.

Take Care.  
\_Michael

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 8:42am UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/16 "2006-04-24T08:42:23Z")

</div>

Looking over this I optimized the code even further… due to the nature of how actionscript handles numbers (and it’s limits as far as points and lines are concerned)

You can replace the isPointOnLine method with the following…

```auto

function isPointOnLine(p1:Point, p2:Point, p3:Point):Boolean {
    var m1:Number = getSlope(p1, p2);
    var m2:Number = getSlope(p2, p3);

	 if ( p3.x > ((p1.x > p2.x) ? p1.x : p2.x) || p3.x < ((p1.x < p2.x) ? p1.x : p2.x) )
	 {
		 return false;
	 }
	 
    return (m1 == m2) ? true : false;
}

```

If you could benchmark this again and post the results that would be awesome. Take Care.

\_Michael

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 24, 2006, 9:26am UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/17 "2006-04-24T09:26:20Z")

</div>

Ok, here are results on 1Ghz PIII, with 10.000 loops

the old one cca 2732 ms  
this new version cca 1793 ms

thats one nice improvement :thumb:

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 12:51pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/18 "2006-04-24T12:51:51Z")

</div>

I figured that it would be significant… Thank you very much matthew.er, I usually have no probelm doing the benchmarking myself, but I yesterday I was in and out all day. You take care, if I come up with any improvements they will be posted here.

TakeCare  
\_Michael

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 2:57pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/19 "2006-04-24T14:57:20Z")

</div>

```auto
function isPointOnLine(p1:Point, p2:Point, p3:Point):Boolean {
     if ( p3.x > ((p1.x > p2.x) ? p1.x : p2.x) || p3.x < ((p1.x < p2.x) ? p1.x : p2.x) )
     {
         return false;
     }

    var m1:Number = getSlope(p1, p2);
    var m2:Number = getSlope(p2, p3);
    
    return (m1 == m2) ? true : false;
}

```

Here the optimization is actually not significant, and you’d only see a performance benefit with large iterations over points that do not exist on the line. I don’t know what I was thinking before ;), because if the point is not within the range on the x axis then there is no possibility that the point exists on that segment, therefore the calls to getSlope() and the allocation of memory are overkill and create unnecessary overhead on invocation of this method.

\_Michael

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 24, 2006, 3:08pm UTC](https://forum.kirupa.com/t/dot-crossing-a-line/186673/20 "2006-04-24T15:08:01Z")

</div>

[ot]lol, Michael, where do you get all that insight on how these things work?[/ot]

[Next page](https://forum.kirupa.com/t/dot-crossing-a-line/186673.md?page=2)
