Skip to main content
December 30, 2007
Question

[Math] Determining perpendicular angle...

  • December 30, 2007
  • 11 replies
  • 1201 views
It seemed like it should be trivially easy, however, the past several hours(!) have proven otherwise, at least for me.

I have a series of points with angles. I want to know which of these points is the "most perpendicular" to a target point. If I have to walk each point, that is fine.

So I have something like:

P1 = {x:25,y:15,angle:45}
P1 = {x:73,y:375,angle:180}
P1 = {x:35,y:135,angle:-56}
etc
targetP = {x:75,y:15,angle:15}

Which P is the most perpendicular to targetP?

I should note that in actually, these points fall along a bezier curve(the angle for each point is the tangent). So they aren't randomly placed.

I've gotten sorta close. I'm walking through the points, performing some basic math so that perpendicular is always 0, then when I "pass over" 0 I know I just passed perpendicular, so in between the two points is perpendicular:

currAngle = angle1-angle2;
if(currAngle>180) currAngle -= 180;
if(currAngle<-180) currAngle += 180;
currAngle = 90-Math.abs(currAngle);
if((currAngle>0 && prevAngle<0) || (currAngle<0 && prevAngle>0)){
// I have just passed perpendicular....?
}

The problem is it isn't always the case. Sometimes, it seems, that the angles go from positive, to 0, then back to positive again. Additionally, sometimes it goes from 90 to -90, thus triggering 'perpendicular' when it isn't actually. My math is just bad. I need a reliable way to determine which point/angle pair is most perpendicular with a given point, accounting for Flash's -180 to 180 angle measurement.

Thanks for any help!
This topic has been closed for replies.

11 replies

Inspiring
December 30, 2007
I'm not sure if I just understand, do you mean something like this?
Hope can help...

/* I suppose ang and target can vary from -180 to +180 and the two
angle are in the same space */
public function mostPerpIndex(angArray:Array,target:Number):uint {
var mostPerpIndex:uint =0;
var mostPerpAngle:Number=360;
if(target<0) {
target=(target+360)%360;
}
for(var i:uint=0;i<angArray.length;i++) {
var testAng:Number;
if(angArray <0){
testAng=(angArray
+360)%360;
} else {
testAng=angArray ;
}
var temp:Number;
if(target>testAng) {
temp = target-testAng;
} else {
temp = testAng-target;
}
var diff:Number =
Math.min(Math.abs(90-temp),Math.abs(270-temp));
if(diff<mostPerpAngle) {
mostPerpAngle=diff;
mostPerpIndex=i;
}
}
return mostPerpIndex;
}