Unfortunately, I just read the actual first posting and.. my response in no way meets the requirements! DOH! Still works pretty well for what it is though.
As far as not "snapping" to the mouseloc, could be treated as an offset instead. Store the angle for the mouseloc on mousedown, then on mousemove calc the offset.
The graphic itself looks very nice, but it would almost be easier (ok, it would definitely be easier) to use just a round graphic, no line. And a separate line as the dial so that it can start at 0 angle being vertical. Makes all the other mental gymnastics MUCH easier.
Once that is done, you can use the following code library for all kinds of neat things. (you can of course adjust for the 90 degree offset, but its a pain in the tookus)
Heres the code library, there is stuff that may not be useful (like the lat/lon distance calc) but here goes anyway:
Code: Select all
## takes 2 points, px,py starting, px2, py2, ending and returns vector offsets
function pointsToVec px,py,px2,py2
return px2-px,py2-py
end pointsToVec
/*
Takes an object reference and returns the x or y position of the object
Examples:
get xLoc(the long id of field "myfield")
get yLoc(the long id of grc "mygrc")
*/
function xLoc pObject
return item 1 of the loc of pObject
end xLoc
function yLoc pObject
return item 2 of the loc of pObject
end yLoc
##Return the length of the vector.
##vX and vY are the offset distances from the starting point
## then uses pythag Theorem to calc the distance
function vLength vX,vY
return sqrt((vX * vX) + (vY * vY))
end vLength
## Takes vector as the offset distance between points for x and y
## calculates the degrees using atan2 * 180 / pi
function vToDegrees vX, vY
return atan2(vx,vy) * 180/pi
end vToDegrees
## takes degrees and changes to radians
function dToRadians pDegrees
return pDegrees / 180 * pi
end dToRadians
## takes radians and converts to degrees
function rToDegrees pRadians
return pRadians * 180 / pi
end rToDegrees
## takes length and the angle in radians and returns vector as x,y offsets
function aLenToVector pLen pAngle
return pLen * sin(pAngle),pLen * cos(pAngle)
end aLenToVector
## Get unit vector.
## Returns vector offsets whos vector length = 1 based on vector x and vector y
## make sure if vector length is 0, handle the error.
function uVector vX,vY
if vLength(vx,vy) is not 0 then
return vx/vLength(vx,vy) ,vy/vLength(vx,vy)
else
return 0,0
end if
end uVector
## return left and right normals
function LRNormals px,py
return -py,px,py,-px
end LRNormals
## add 2 vectors
function vAdd vx,vy,vx2,vy2
return vx + vx2,vy + vy2
end vAdd
## find the dot product of 2 vectors
## positive dp means the vectors are additive
## negative means they're subtractive
## IE positive they go generaly the same direction
## negative they go generally the opposite direction.
## 0 = perpendicular movement
function dotProd vx,vy,vx2,vy2
return vx*vx2 + vy * vy2
end dotProd
##Projection-- still learning so not sure yet where this falls into things.
function vProj vX,vY,vX2,vY2
return dotProd(vx,vy,vx2,vy2) * vx2,dotProd(vx,vy,vx2,vy2) * vy2
end vProj
## pR is radius, the lats and lons are decimal reperesntaions of the start and end point
function calcDist pR,plat1,plon1,plat2,plon2
## convert to radians
put plat1 / 180 * pi into lat1
put plon1 / 180 * pi into lon1
put plat2 / 180 * pi into lat2
put plon2 / 180 * pi into lon2
put lat2 - lat1 into dlat
put lon2 - lon1 into dlon
put (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 into a
put 2 * atan2( sqrt(a),sqrt(1-a)) into c
return pR * c
end calcDist