javascript - For the point inside circle, find in which quarter it is? -
i researched google couldn't find keywords search. ask here if algorithm , code efficient?
http://sketchtoy.com/66429941 (algorithm)
the algoritm is: have 4 points are: north, east, south , west of circle. check 4 distances (distancetonorth, distancetoeast, distancetosouth, distancetowest). , find minimum of them quarter.
here code not seem efficient me. (firstquarter north, secondquarter east , on..
note: assume mousemove inside circle.
var firstquarterx = centerx; var firstquartery = centery - radius; var secondquarterx = centerx + radius; var secondquartery = centery; var thirdquarterx = centerx; var thirdquartery = centery + radius; var fourthquarterx = centerx - radius; var fourthquartery = centery; var distancetofirst = math.sqrt(math.pow(x-firstquarterx, 2) + math.pow(y-firstquartery, 2)); var distancetosecond = math.sqrt(math.pow(x-secondquarterx, 2) + math.pow(y-secondquartery, 2)); var distancetothird = math.sqrt(math.pow(x-thirdquarterx, 2) + math.pow(y-thirdquartery, 2)); var distancetofourth = math.sqrt(math.pow(x-fourthquarterx, 2) + math.pow(y-fourthquartery, 2)); var min = math.min(distancetofirst, distancetosecond, distancetothird, distancetofourth); var numbers = [distancetofirst, distancetosecond, distancetothird, distancetofourth]; var index = numbers.indexof(min); // give 0 or 1 or 2 or 3 var quarter = index + 1;
observe boundaries between quarters lie along lines equations y = x
, y = -x
, relative origin @ center of circle. can use evaluate quarter each point falls in.
if point (x
, y
), coordinates relative center of circle xrelative = x - centerx
, yrelative = y - centery
.
- your point in first (south in code) quarter if
yrelative < 0
,math.abs(xrelative) < -yrelative
- your point in second (east) quarter if
xrelative > 0
,math.abs(yrelative) < xrelative
- your point in third (north) quarter if
yrelative > 0
,math.abs(xrelative) < yrelative
- your point in fourth (west) quarter if
xrelative < 0
,math.abs(yrelative) < -xrelative
i leave determine quarter assign points fall on boundary. also, can implement little decision tree based on criteria if prefer; should little more efficient testing each criterion in turn.
Comments
Post a Comment