java - Position as time on a clock based on the coordinates -
based on x , y coordinates, need implement function like
int getarea(double xcoord, double ycoord , double radius)
to rough time on clock. rough time means time on clock in full hours. in case 1 o'clock, 2 o'clock, etc... if coordinates outside of radius, function should return -1.
or more graphic: imagine second hunger games movie/book. have 12 different areas, sorted clock. , enter coordinates of tribute function, radius of arena , in return want have area tribute in.
so managed figure out how check, whether position on clock or not
if(math.pow(x,2) + math.pow(y,2) < math.pow(radius,2))
also, have little piece calculates sectors:
int sectors = 12; double angle = 2*math.pi/sectors; double x_sectors[] = new double[sectors]; double y_sectors[] = new double[sectors]; for(int = 0; i<sectors; i++){ x_sectors[i] = math.cos(i*angle)*radius; y_sectors[i] = math.sin(i*angle)*radius; }
but i'm stuck method how check in sector given coordinates are.
i recommend use math.atan2 method , shift range of angle:
int getarea(double xcoord, double ycoord , double radius) { if(xcoord*xcoord + ycoord*ycoord > radius*radius) return -1; double angle = math.pi/2 - math.atan2(ycoord, xcoord); // need suptract angle pi/2 because want 0 rad @ +y axis instead of +x axis if(angle < 0) // math.atan2 gives angle in range -pi/2 pi/2 need shift range 0 2*pi angle = 2*math.pi + angle; int segments = 12; double angle_one_segment = 2*math.pi/segments; return 1 + (int)(angle/angle_one_segment); // 12 o'clock 1 o'clock it's first sector (exactly 12 belongs 1st sector) , on. if point (x, y) lies @ boundary between 2 sectors belongs higher 1 }
Comments
Post a Comment