public
Created

Solving for angle

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
//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);
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.