Skip to content

Instantly share code, notes, and snippets.

@sdailey
Created September 18, 2012 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdailey/3741816 to your computer and use it in GitHub Desktop.
Save sdailey/3741816 to your computer and use it in GitHub Desktop.
Solving for angle
//please don't use this in building the next space shuttle :)
//var vertex is an array which has all our points to build our polygon with
testPolygon = new google.maps.Polygon({ paths: vertex, fillColor: color, geodesic: true, fillOpacity: 1, strokeColor: '#00ff00', strokeWeight: 1, strokeOpacity: 0.6 });
count= 0;
while (count<vertex.length){
//lets check the angle between the last two vectors
if (count == vertex.length - 2){
console.log('count is at ' + count);
point1 = testPolygon.getPath().getAt(count);
y1 = point1.lat();
x1 = point1.lng();
point2 = testPolygon.getPath().getAt(count+1);
y2 = point2.lat();
x2 = point2.lng();
point3 = testPolygon.getPath().getAt(0);
y3 = point3.lat();
x3 = point3.lng();
//Calculate vectors
vx1 = x2 - x1;
vy1 = y2 - y1;
vx2 = x2 - x3;
vy2 = y2 - y3;
cw_or_ccw = ((vx1 * vy2) - (vx2 * vy1)); //cross product
angle = Math.acos((vx1 * vx2 + vy1 * vy2) / ( Math.sqrt( vx1*vx1 + vy1*vy1 ) * Math.sqrt( vx2*vx2 + vy2*vy2 ) ));
//Take into account cross product (winding direction)
//the angle is in radians, i converted it to degrees
if(cw_or_ccw > 0){ // if crossproduct is positive
ourPointsAngle = 57.2957795*angle;
} else { // if crossproduct is negative
forCCW += 57.2957795*angle;
ourPointsAngle = 360-57.2957795*angle;
}
console.log(ourPointsAngle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment