Created
September 18, 2012 07:38
-
-
Save sdailey/3741816 to your computer and use it in GitHub Desktop.
Solving for angle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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