I need to draw an arc in pygame, through three points, however pygame's arc function doesn't seem to support this. I have researched three point arcs, both in and out of pygame, however cannot find anything that I understand and is useful.
I would ideally like a full code however any hints will be useful
Here are some hints, which will require a little mathematics to finish. If you need more help, show more of your work and ask.
Let's say you want to draw a circular arc from point a=(ax, ay) through point b=(bx, by) to point c=(cx, cy). You want to use the function
pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)
So you need to find Rect, the bounding rectangle (which will be a square) of the circle, and the start and stop angles. Here is an outline.
Use your point coordinates to calculate the center of the relevant circle, giving you point u=(ux, uy). If that calculation raises a divide-by-zero or overflow error, the points are (nearly) in a straight line, so just draw a line segment from point a to point c and you are done.
Calculate the distance from point u to point a (or b or c, it doesn't matter). That will be the radius of the desired circle.
Use point u and the radius to calculate the parameters of the bounding rectangle Rect. The rectangle will actually be a square, and point u will be its center.
Use a little trigonometry to calculate the direction angle from point u to point a: this (almost) will be start_angle. Be careful, since Cartesian coordinates have increasing y go up, while in pygame increasing y goes down. Also calculate the direction angle from point u to point c: this (almost) will be stop_angle. Then calculate the direction angle from point u to point b.
Examine those three direction angles. If necessary, swap the start and stop angles and/or add 2*pi to the stop angle to ensure that the start angle is less than the stop angle and that the arc goes through point b.
You now have Rect, start_angle, and stop_angle. This outline ignores some subtle issues such as rounding to integers, but I'll leave those to you.
Related
from turtle import Turtle,Screen
t=Turtle()
s=Screen()
t.left(20)
from cmath import pi
print(pi)
circle=2*pi*40
print("circle=",circle)
t.circle(radius=50,extent=2*pi*50)
turtle docs says that
turtle.circle(radius, extent=None, steps=None)
Draw a circle with given radius. The center is radius units left of the turtle; extent – an angle – determines which part of the circle is drawn. If extent is not given, draw the entire circle. If extent is not a full circle, one endpoint of the arc is the current pen position. Draw the arc in counterclockwise direction if radius is positive, otherwise in clockwise direction. Finally the direction of the turtle is changed by the amount of extent.
As the circle is approximated by an inscribed regular polygon, steps
determines the number of steps to use. If not given, it will be
calculated automatically. May be used to draw regular polygons.
I am working with Python and currently trying to figure out the following: If I place an ellipsis of which the semi-axes, the centre's location and the orientation are known, on a pixel map, and the ellipsis is large enough to cover multiple pixels, how do I figure out which pixel covers which percentage of the total area of the ellipsis? As an example, let's take a map of 10*10 pixels (i.e. interval of [0,9]) and an ellipsis with the centre at (6.5, 6.5), semi-axes of (0.5, 1.5) and an orientation angle of 30° between the horizontal and the semi-major axis. I have honestly no idea, so any help is appreciated.
edit: To clarify, the pixels (or cells) have an area. I know the area of the ellipsis, its position and its orientation, and I want to find out how much of its area is located within pixel 1, how much it is within pixel 2 etc.
Following the equation of an elipse
The easiest way to find which pixels from your mesh are inside and which are out would be to assign (x, y, alpha) for each pixel in the above equation.
If the result <=1, the pixel is inside. Otherwise, it is outside.
You can count the pixels.
This is math problem. Try math.exchange rather than stackoverflow.
I suggest you to transform the plane: translation to get the center in the middle, rotation to get the ellipsis's axes on the x-y ones and dilatation on x to get a circle. And then work with a circle on rhombus tiles.
Your problem won't be less or more tractable in the new formulation but the math and code you have to work on will be slightly lighter.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Improve this question
I have a circle ( Striker in my game ) which moves along my 2d plane from one point to another point along a straight line. I have some other circles in the same plane which are not moving. What is the best algorithm to find the list of circles which will intersect the moving circle during its motion. If I want to code this in python what is the best approach ( Memory efficient as I want do this for lot of different states for my problem ) ?. What is the best library to be used for this purpose ?
You asked for an algorithm, rather than code, so here is the idea. You may want to use the math module for this, but nothing else is really needed if you know basic American 11th grade analytic geometry.
There are two ways a stationary circle could intersect your moving circle: it could intersect with the moving circle at the start or end of its path (the red circles centered at A and B in the diagram below), or it could intersect the rectangle that is formed by the moving circle (the blue rectangle marked EGHF in the diagram).
Checking intersection with the end circles is easy: circles intersect if the distance between their centers is not greater than the sum of their radii.
Checking intersection with the rectangle is a little harder. The circle at I intersects rectangle EGHF if two requirements are fulfilled. First, the center I must be between the lines GH and EF forming the ends of the rectangle. Second, the distance from I to line AB (the line enclosing the path of the moving circle's center) must be not greater than sum of the radius of the stationary circle at I and the radius of the moving circle.
An alternate, more consistent way to check intersection with the rectangle is to check to distances from the center of the stationary circle: to the perpendicular bisector (the magenta line marked p) of the start and end points of the moving circle, which must be at most half the distance between points A and B, and the distance to line AB, which must be at most the sum of the radii of the moving and stationary circles.
All those calculations (distance between two points, equations of the lines such as GH, EF, AB, and a perpendicular bisector, seeing if a point is between two parallel lines, and the distance from a point to a line) are standard in analytic geometry. Most of those calculations depend only on the moving circle--the calculations for each stationary circle are few and quick. Let me know if you need help with those formulae. There may well be a module for such formulae, but I am not aware of one.
The memory involved in this method is minimal, assuming that the radius and the start and end center coordinates of the moving circle, as well as the radius and center coordinates of the stationary circles are already in memory. If you choose my alternate approach, you could save execution time by pre-computing some values for the moving circle. If the start coordinates of the circle's center are (x1, y1) and the end coordinates are (x2, y2), you would pre-compute:
dx = x2 - x1
dy = y2 - y1
d = math.hypot(dx, dy)
det = x2 * y1 - x1 * y2
c = (x1 * x1 + y1 * y1 - x2 * x2 - y2 * y2) / 2
(Note those five values are stored only once, for the moving circle's path.) Then for each stationary circle, find the signed distance from that circle's center to the perpendicular bisector (p in my diagram), then use that to find the distance to the start circle, end circle, or line of movement. If you have many stationary circles and the path of the moving circle is small compared to the playing field, you could use a bounding rectangle for the path to quickly eliminate most of the stationary circles.
I'm trying to rotate a rectangle based on the position of the mouse inside or outside of the circle.
The way I see it, if I can determine the point on the circle that is closest to the position of the mouse, I can then transform the rectangle along the circle using that point as the target.
I cannot however, figure out how to find that position. I thought that perhaps by using y=mx+b to follow the line from the mouse pos until it hits the point on the circle.
The problem with this however is that I do not have all of the points on the circle and there are hundreds if not thousands of points on the circle.
If the mouse position is outside of a circle, how do I find the point on the circle closest to the mouse-position?
Use math.atan2() to get the angle of the cursor from the center. The circle will be a fixed distance from the center, so you can just convert the angle and distance to a point on the circle with more trig.
angle = math.atan2(ymouse - ycenter, xmouse - xcenter)
I am trying to rotate a vtk camera around its focal point. The aim being to 'orbit' the model.
I'm using the camera.SetPosiiton(x, y, z) call to set the camera location, and I know I can do the same at each update period in my render window.
The focal point has the location (0, 0, 0), and some other bounding box getting gives me my initial camera (x, y, z) location. The distance from the focal point (0, 0, 0) to the camera location 9x, y, z) describes the radius the of the sphere.
In my head, this essentially moving the camera in steps around the point (0, 0, 0) and I am presuming there is a maths function I could use to feed it my starting camera point, and work out my next camera location.
This should result in the model appearing to spin in space. My camera view is offset from all x, y, z, planes, making it a 3d problem, not a 2d problem. However, I do want my camera to remain the same distance from the model (focal point)
What I am trying to achieve is like this:- take a pencil (my model is long and narrow). Hold it in your finger tips at arms length, tip pointing to the ceiling. Tilt the pencil by ~30 degrees. This is the camera start position. Rotate the pencil body in your fingers, maintaining tilt angle, and the distance from your eye.
THis post looks helpful: Plotting a point on the edge of a sphere however, this assumes you know the radius to get to the initial x, y location.
Could some one point me towards the maths I need to do this, my maths is horribly rusty.
It seems what you want is to rotate a vector about an axis, this can be most easily done using a rotation matrix
So, if your desired axis of rotation is tilted 30 degrees from the z axis on the zx plane, your axis of rotation is (cos(pi/6),0,sin(pi/6)), increment the rotation angle, plug that into the rotation matrix to get matrix R, the new camera position vector will be R*(x,y,z)'
Start off with the points (+-1,0,0), (0,+-1,0), (0,0,+-1). These form two Pyramids with all the points on the unit sphere.
Now you can take the midpoints of each triangle, and project out so they lie on the unit sphere too. For each triangle this now gives you 3 new triangles, and you can repeat the process.
The alternative to the midpoint of the triangle is to take the midpoints of each side, and join them up. That gives 3 new points that can be projected out to the unit circle. This gives you 4 triangles for each sub division.
Repeat as many times as you need.