i have to draw circle with free hand in onDraw() method.
After drawing the circle, i have to find the radius and center of that circle and show that circle again with drawCircle() method.
Can anyone help me to find the radius and center of circle with free hand drawing.
You could take the center as the average of all the points on the edge.
And the diameter as the largest distance between any two points.
Or once you have the center, find the distance between each edge point and the point across from it. Then average these.
Related
As background, I am new to Android graphics. I'm trying to create a rectangle with rounded corners using Path specifically (I don't want to use method addRoundRect as I will make changes to my Path object later to not be rectangular). I want this to have the same curvature as a shape with corners with a radius of 12 dp. I'd like to use the methods rQuadTo or quadTo (based off of this question), but am a bit confused how to get the corners to match each other perfectly. Can someone explain the math behind how to to achieve this and what setting the radius exactly mean for drawable resource shape (is this a correct definition?)? Visuals would be help as well! Thanks.
Yes, link contains correct definition.
To build rounded corner with quadratic Bezier quadTo, you should start curve (end straight line) at distance r=12 before corner position, make control point exactly at corner position (to provide symmetry) and make end point at distance r after corner at perpendicular edge. Quadratic Bezier curve does not give perfect circle arc, but it is not significant for small sizes.
Example:
Horizontal edge in right direction to corner 100, 100.
End point of line is 88, 100. (and starting point of curve)
And quadto(100, 100, 100, 112)
I would like to detect collisions between shapes dynamically drawn on a canvas (SurfaceView) for an Android game.
I can easily use intersect method of Rect or RectF objects but the result is not very good (see picture below where I have a "false" detection).
I don't want to use Bitmap so it's impossible to use the "pixel perfect" method.
Do you know a way to do this for circle, rect, triangle and other basic shapes intersection ?
Thx for help ;)
For a good collision detection you have to create your own models behind. In those models you specify the conditions that two objects colide.
For example, a circle is described by the center position and by the radius. A square is described by the left down corner and by the edge length.
You don' t have to describe all possible poligons, you can use the so called bounding boxes, meaning that, for a complex random poligon you can use a square or whathever shape fits it best(also you can use multiple shapes for a single object).
After you have the objects in mind you compute the condition that each one of them will colide with all other shapes including itself.
In your example The sphere and the square colides if the distance between any corner of the square is greater than the circle's radius.
Here you can read more http://devmag.org.za/2009/04/13/basic-collision-detection-in-2d-part-1/
This problem can get very complex, keep it simple if you want something simple.
Here is a directly applicable method I use in my own game to detect circle and rectangle intersection. It takes the ball (which is a view in this case) and the rectangle (also a view) to be checked for collision with the ball as parameters. You can put the method in a Timer and set the interval you want the circle and rectangle to be checked for collision.
Here is the method:
public boolean intersects(BallView ball, Rectangle rect) {
boolean intersects = false;
if (ball.getX() + ball.getR() >= rect.getTheLeft() &&
ball.getX() - ball.getR() <= rect.getTheRight() &&
ball.getY() + ball.getR() <= rect.getTheBottom() &&
ball.getY() - ball.getR() >= rect.getTheTop())
{
intersects = true;
}
return intersects;
}
getR() gets the circle's radius
getX() gets the center of the circle's X position value
getTheLeft() gets the rectangle's left X value
getTheRight() gets the rectangle's right X value
getTheTop() gets the rectangle's top Y value
getTheBottom() gets the rectangle's bottom Y value
If you can't directly use this method in your code you can still conjecture the logic it entails to implement it where it would work for you. It detects all collisions without using pseudo-collision detection like a collision box for the circle.
Good luck! And if you have any questions feel free to ask, I'm here to help!
To know if a polygon in 2d is colliding with a circle, you can test, for each of its lines, where is the point on the line that is closest to the center of the circle (this might help).
Then, check if the point you found is between to two corners that make the line - that is, that the point is actually on the line, and not just on its continuation - and if the distance of that point to the center of the circle is smaller or equal to the radius of the circle. If both are true for any of the lines of the polygon, you have a collusion. You also have to check for the edge cases where the corners of the polygon might be in, or touching the circle.
For two circles, this is easier. Check the distance between the centers, and compare it to the sum of their radiuses. If the distance is smaller or equal to the sum, you have a collusion.
I get too points A and B, and I want to draw an arc that passes over the two points (in other words, determinate the circle center coordinate C).
I know the radius, and angle, and the two points coordinates.
I hope I'm clear.
Could you help me ?
Thanks.
You know the distance from two points to the center of the circle (the radius).
Draw two circles with the same radii but with center of these two known points A and B. Where these two circles intersect is the center of the circle you are looking for.
I'm not sure what you mean by "I know the [..] angle"
I am trying to create custom components in Android using Surfaceview and canvas drawing. The components are re-sizable and rotatable by touching. Consider creating an image view its Top,Right,Bottom, and Left edges are scalable by touching and dragging the required edge. I am using RectF to keep the bounds of the component, For rotation I am using canvas.rotate(angle, bounds.centerX(),bounds.centerY()) method. The problem is while resizing top edge, the Let , Right and Bottom edges should be fixed, and I cannot able to fix it if the rotation angle is other than 0 degrees. I need a math solution to find out the x,y coordinates of a rotated rectangle with respect to the actual rectangle's bounds.
I can explain it with the help of some images.
The following Figure displays two rectangles whose bounds are also known and displayed in respective colors. Consider the Green Rect as the components initial bounds, ie. rotated by -45 Degrees, Center is (10,10). Now going to re-size the Top edge of the rectangle and displayed in next Figure 2.
From the Figure 2 it is understood that the Y position is reduced to 4 from 6. The rotated Rectangle is also shown in pink color. Remember I am doing resizing while the component is at rotation angle -45 degrees, so while dragging Top Edge rectangle's Left, Right and Bottom positions should not be changed. So the Figure 2's Pink Rectangle should have Left, Right, and Bottom coordinates same as Figure 1's Green Rectangle. Comparison of the obtained and expected rectangle is shown in Figure 3.
In Figure 3 the yellow color rectangle is the Expected/Required out put. The obtained rectangle Pink color is shifted upwards compared to the Green rotated rectangle and that is varying depends on the Angle of rotation.
I have rotation angle = -45 degree
Bounds of Actual (Not re-sized) rectangle.
Bounds of Actual (Not re-sized) rectangle at Rotation = -45 degrees.
Bounds of Re-sized rectangle.
Bounds of Re-sized rectangle at Rotation = -45 degrees.
How do I calculate the Bounds / Center of the Yellow Rectangle. So that I can implement the resizing of my components correctly? Let me know is there is any mathematics that can be applied?
The required points / coordinates are marked as Red color circles in Figure 3.
The key is this: "I cannot able to fix it if the rotation angle is other than 0 degrees."
Let's say your rectangle is rotated 10 degrees.
1) rotate the mouse coordinate around some point on the screen by -10 degrees
2) rotate the center of the rectangle by -10 degrees
... now you reduced the problem to a rectangle that is at 0 degrees. The rectangle moved, yes, the mouse moved, but they are relative to each other as they should be.
3) Now do the the rectangle manipulation. The rectangle center will shift.
4) Rotate the new rectangle center by 10 degrees
This way you do not have to think about it and you are always working in un-rotated coordinates.
Point at [x, y] rotated by angle a will end up at [x*cos(a) - y*sin(a), x*sin(a) + y*cos(a)]
All colors in this answer refer to your figure 3.
If I understood your question correctly, you know how to compute all the details about the pink rectangle as well as the green rectangle. So simply take the difference between one corner of the pink rectangle and the corresponding corner of the green rectangle. Adding that difference (a two-element vector, i.e. x and y difference separately) to the center of the pink rectangle will give you the desired center of the yellow triangle.
If you need to compute the dimensions of the pink rectangle as well, you might want to do so in the unrotated coordinate system. Take your green rectangle together with the coordinates of the point towards which you want to extend the rectangle, and rotate them back by +45°. Then you can extend the height of the rectangle to the value you desire, which will give you the blue rectangle, and from that by rotation the pink rectangle.
I am trying to make a map overlay that just shows a solid circle from the map center who's radius is a range in meters.
I can't figure out how to calculate the circle's radius. I can get the map center, but I haven't had any luck figuring out how to convert meters into the proper units for the circle's radius.
Thanks for any help.
I've got no experience of doing this in a gMaps context, but the MapView class (whose getProjection() I'm assuming you've used to work out where your point is on the screen) has getLatitudeSpan() and getLongitudeSpan() which you can use against the screen res to calculate the pixel-radius of your circle.