This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find angle of a point from center of circle
Imagine a circle, the center point is fixed, as is the point at the top of the circle. What i want is to give a third point anywhere around the circle (or outside the circle) and get the degrees from the center to top point line from 0-359. (I actually graphed out a nice picture illustrating but im new and cant post it)
To give some context, the circle is a little planet and I want do place a little building on the surface with the base of the building at a tangent. I need the rotation angle for my building bitmap.
edit: Thanks for the help, i'm still struggling with this one though. I wonder could it be relevant that I'm using android and the y0 coordinate is at the top? Is it the other way around on other platforms? would that affect the calculation?
Solution: Because I am in android and the y coords are counted from top to bottom I had to change a - witha +
degrees = Math.atan2(x - centerX, -y + centerY);
// and to make it count 0-360
if (degrees < 0) {degrees += 2 * Math.PI;}
Use Math.atan2() to get the angle in radians from east, and then rotate and convert as appropriate.
Note that atan2 is defined as atan2(y, x) NOT atan2(x, y), as you might expect.
Get the horizontal distance and the vertical difference between the center and the point, divide one by the other, and pass the result to the method Math.asin(double).
The result will be the angle in radians. When you need it in degree, you can use the method Math.toDegrees(double). Most APIs I know prefer radians, though.
Related
I am working in an android application where i need to draw a graph like this.
I have drawn the arc using paint and canvas but i didn't know how to draw the line path along with the text as mentioned in below picture!
Any heads up on this would be really helpful for me. Thanks in advance.
In order to keep direction of the line truly, you have to use many trigonometric functions and calculus. However, for such cases you can use canvas.rotate() for tricky solution. For the solution, first you calculate angle of line according to a value. For example, assume your arc represents total value of 200. The left side is 0 and the right side is 200, then you get the value of 80. With these values, you can calculate the angle like that 180degree * (80f)/(200 - 0) it gives 72 degree. Then you can rotate the canvas for drawing canvas.rotate(70f,centerX,centerY). CenterX and CenterY are values of the center point of the arc. After that, you can draw your line as you draw to line at the left-bottom corner of the canvas canvas.drawLine(0,100,20,100,paint).
canvas.save()
canvas.rotate(70f,centerX,centerY)
canvas.drawLine(0,100,20,100,paint)
canvas.restore()
I followed an online tutorial, I did not understand how the professor managed to get the distance of a circle from the top left corner of the screen:
//calculate the distance from epicenter (of a circle) to the top left corner of the screen
double theta = atan(epicenter.dy/epicenter.dx);
double distanceToCorner = epicenter.dy / sin(theta);
I would like to know how to get the distance from all the other screen corners (and possibly have an explanation of what has been done).
Thank you
Assuming, that in android you can get screen width and height, you can simply count the distance at horizontal and vertical axis separately.
Getting the distances at those axes, you can use Pythagoras equation, like
dist = sqrt( dx^2 * dy^2 )
To make it more felxible, just make a function, that takes corner position as a parameter and make the dx and dy as a absolute difference of corner and epicenter location.
Going back to your question and atan(...), I don't quite understand the need of using this here, except if that's a project for the math class :)
I know this is not the answer but if i understand what you mean, then this image might be helpful.
I am currently working on a project in which i need to draw a non-right triangle in the center of a JFrame using either, java.awt.Graphics's drawLine() or drawPolygon() methods. Both of these methods require the coordinates of all of the points to function. My problem is that instead of points, all i have are all of the angles and side lengths of the triangle. I've drawn up a nifty diagram of what I hope helps you visualize my problem:
(EDIT the position of C in this Senario is not fixed betwen a and b and may be anywhere below the axis on which AB rests)
as you can see from my picture, I need the coordantes of C based off the coordanes of A, is there any way to calculate this given the lengths of all sides and angles of the non-right triangle?
Bonus: How would i find an (x, y) value for A that would effectivly center the triangle in the middle of the JFrame?
If you know angle CAB, the coordinate of point C should be:
(x+b·sin(θ), y-b·cos(θ))
In Java, there is:
double Math.sin(double radians);
double Math.cos(double radians);
Keep in mind that the angle needs to be in radians. If your angles are in degrees, try:
double Math.sin(Math.toRadians(double degrees));
double Math.cos(Math.toRadians(double degrees));
Hope this helps.
I'm building a prototype android app and I'm trying to make a circular layout. Basically, I have a centre point, and I want to be able to place other elements in a circle around it, like this. There's a library called ArcLayout that I tried using, and it works well, but it doesn't quite work for what I want to do. The elements I'm trying to place have a dynamic distance from the centre, and may have similar or identical distances at any given time.
I could just create a new arc layout for each distance level, but I'm trying to avoid something like this. Ideally, each element in the layout should position itself at a maximum distance from all other elements, while staying at the correct distance from the centre.
I've looked up a lot of different things from radar graphs, to orbiting animations, and none of them seem to work with what I want to do. How would I go about making something like this?
Here is a high level description of how I would do this:
Make your original view extend View class. In your ondraw method, do the following:
first draw the circle in the center of the view by getting the coordinate for the small circle by getWidth()/2, getHeight()/2. set some fixed radius. Store the center as cx,cy
Now, you need to draw other circles at the right position, for that you need the center of each circle. Now, from cx,cy if you have the angle it makes with x - axis and the distance from cx,cy to the new point, you can get the coordinates to the new points as follows:
cx1 = cx + r*cos(theta) and cy2 = cy + r*sin(theta).
where r is the distance between the points and theta is the angle between line joining the points and the positive x-axis.
Just remember to convert degrees to radians in your calculations.
Once you get coordinates for the new circle, just draw it using canvas.drawCircle method.
Repeat this method as often as needed.
I have a problem. I have an array tag that includes the outline of a polygon, each point has gps coordinates.
I would like to find the focus point is in my array.
How can I do that?
I would like to have a gps coordinates of a red point (just one no matter).
Edit:
I have a area, i want center my zoom in a map at the middle of this area. This area is represented by a polygon of point. I want the coordinates of this point.
Loop through your points and remember the smallest and greatest x, y coordinate. After that your x-coordinate is xcenter = (xmax-xmin)/2 and ycenter = (ymax-ymin)/2. That's the geometrical center.
If you want the 'Centroid of polygon' you can take the formula discussed here: https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon (it's a sumformula over the coordinates).