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.
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'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 am building a Java application that is going to feature two circles of random sizes that need to be clicked by the user. The time between the click on the first and the second circle is going to be measured. Unfortunately, since I am new to Java so things have been slow for me. Currently I have my application draw circles and measure time between clicks using System.nanoTime() but now I am running into a problem.
Because the circles need to be a fixed distance away from eachother I want to use the center of the circles as the origin points. So basically I want to be able to provide coordinates for the circle so that the center of the circle should be at those coordinates. The distance between the circles then describes the distance between the centers. My circle currently is embedded into a JPanel but if I set the JPanel's position it moves the top left to that position.
Of course I have done some searching read that I may need to play around with either AffineTransform or Graphics2D.translate() which I have tried in paintComponent() but this got a bit confusing so then I tried to override setlocation and subtract the radius from the position. It sort of works but it is not the most clean solution. Can aonyone give me some pointers on how to do this?
Thanks in advance.
If I understand the problem statement, all such pairs of circles will lie on opposite sides of a circle centered in the enclosing panel, as shown here. Simply choose a random 0 ≤ θ < π and find its opposite at π - θ. Note how the example's rendering scales as the panel is resized.
As an aside, the example uses setPreferredSize() to establish the dimensions of the drawing panel, but you may want to override getPreferredSize() instead.
Addendum: The example uses fillOval() to render the circles, but you can use draw() with any desired Shape; the latter provides several contains() methods suitable for hit testing, as mentioned here.
You have the coordinates for the two center for the circle (x1, y1) and (x2, y2).
The size of the radius is random.
Once you have the radius of the two, r1 and r2, simply position them at (x1-r1, y1-r1) and (x2-r2, y2-r2).
You can use java.awt.Point to represent the center, and use
center.translate(-radius, -radius)
and use the new translated value as position for the drawing.
Maybe you think it is not a clean solution, but why not? Everything in Java is painted by giving the top left corner for the position, so is the use of the center that is not clean :).
To calculate the left top position by doing -radius is clean :)
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.
I'm writing a game of sorts to practice some programming, and i've come across this problem. In my game, there are circles on the screen. And when the user clicks on the screen, the circles should move away from the click. I've got the x and y position of the point where the mouse button was pressed, and i have the x and y position of each cicle object.
I found the center of the circles with the following code
float cx = circle.getX()+circle.getRadius();
float cy = circle.getY()+circle.getRadius();
And to find the distance from the edge of the circle to the mouse click I did this
float distance = (float) Math.sqrt( ((cx-x)*(cx-x)) + ((cy-y)*(cy-y)) ) - circle.getRadius();
Now after I check if the circle is close enough to the click, how can I split a velocity of 1f, for example, to the circle's variables vx and vy?
EDIT: Well actually I wanted acceleration, but I guess it's all the same.
This sounds like a job for sin and cos in java.lang.Math : http://download.oracle.com/javase/6/docs/api/java/lang/Math.html .
Once you know the total velocity (1f in your example above) and the angle (in radians), the horizontal component of the velocity is v * cos(angle), and the vertical component is v * sin(angle).
You probably need to negate the angle if you want to move it away.
To calculate an angle from horizontal and vertical distances, use atan2.
Btw, if you don't want to take unnecessary square roots, and want to avoid the cost of computing series the way trigenometric functions do, take a look at http://www.youtube.com/user/njwildberger#p/u/368/9wd0i44vK04 .
Find the line from the mouse to the center of the circle and that should be the "force" vector. This vector will give you the direction, now you just need to figure out how distance affects the magnitude.
You could first find angle as Mike suggested and use cos and sin functions.
Or use:
velHoriz = velocity * (cx-x) / distance
velVert = velocity * (cy-y) / distance