what does extent mean for the function circle? - python

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.

Related

Start pixel of turtle python simulator

The turtle simulator is quite helpful for moving objects but the problem I am facing is that I don't know the pixel value of the screen where the 'turtle arrow' starts its drawing. Also, when it comes to drawing a circle, it becomes difficult to figure out the pixel coordinates of its centre. Here is an example of a code:
import turtle
ob = turtle.Turtle()
ob.right(100)
#Where does the turtle start with its head (pixel coordinates)?
ob.circle(5)
#Now the turtle draws a circle with radius 5, but in which direction will it point at first?
#How do we figure out the centre of this circle?
Could someone please help me with these two problems?
PS: I am using python 3.10
import turtle
ob = turtle.Turtle()
ob.right(100)
Where does the turtle start with its head (pixel coordinates)?
Turtles start out at (0, 0), the origin. Since you didn't move the turtle, it's head is still over the origin.
ob.circle(5)
Now the turtle draws a circle with radius 5, but in which direction will it point at first?
The turtle starts drawing the circle in whatever direction the turtle is currently pointing. For a newly hatched turtle, that's 0 degrees which is right on the screen. (Using mode 'logo' changes this default.)
Since your turtle first turned to the right 100 degrees, it will start drawing at 260 degrees heading (360 - 100), slightly to the left of straight down (i.e. 270 degrees.)
How do we figure out the centre of this circle?
If the circle was drawn with a newly hatched turtle, then the center would be at (0, 5). (To center the circle on (0, 0), for example, we'd move -5 (i.e. -radius) pixels in the Y direction.)
But your turtle started with a heading of 260 degrees. And, by default, circles are drawn counter-clockwise. So we'd expect the center of your circle to be near (5, 0), where a 270 degree heading would draw it. If we do the math, turning 90 degrees towards the center of the circle and projecting a 5 pixel line, we get:
from math import cos, sin, radians
print(5 * cos(radians(260 + 90)), 5 * sin(radians(260 + 90)))
With output: 4.92403876506104 -0.868240888334652
Similarly, we can also get the center position by doing:
ob.left(90)
ob.penup()
ob.forward(5)
print(ob.position())
With output: (4.92,-0.87)

how to create a three-point arc in pygame

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.

Python Shapes on grid

Have to make a program that given the option to input square or circle, user inputs width and a center x,y coordinate.
What I don't understand is how to write code for if there are two shapes on a plane and how to identify if one is inside the other
I'm super helpless, and have no background in computer science. Thank you!
You have to address Circle in Circle, Circle in Square, Square in Circle, and Square in Square. I suggest drawing some pictures with the centers marked, and observe their relationships.
Circle in Circle: the distance between the centers has to be less than the difference in radius's.
Circle in Square: Same as circle in circle I think
Square in Circle: The radius of the circle must be larger than the distance from the center of the square to a corner PLUS the distance between centers
Square in Square: you got this! solve yourself ;)

Circle transformation outside of circle

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)

Drawing Arcs in Pyside

I'm trying to do draw widgets on the circle, for this I need to paint the widgets as a arc. I know the number of widgets (let's say), then each widget is at 36 degrees from the origin to the circumference. The information I have is the center of circle, radius and I know the starting and end point on the circumference for each such widget.
This is computed by doing
dx = int(round(400 + 300 * np.cos(angle)))
dy = int(round(400 + 300 * np.sin(angle)))
where angle = 2 * np.pi / 15
I go over a for loop computing the new value for angle which is basically angle * i where i = (1, 10)
I don't understand the start angle and span angle for the arcs function in QPainter.QPainter Arc. I googled and not many terms came up. Maybe there is a different term for them.
So the problem is I have a starting point and ending point on the circumference and center and radius, how do I use them to draw Arcs such that I get something that looks like :
circos
What I have tried is, I can compute the center point (cx) of the two end points, if I draw a line from the center of the circle to this point cx, then I can compute how far this point circumference which essentially is my width, but how to get the orientation correct to represent them as circles.
Instead of circular I do have a layout with just lines like this, but would like to be like the circos one.
My image
I don't understand the start angle and span angle for the arcs function in QPainter.QPainter Arc.
Why? The documentation is IMHO very clear:
The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360).
This means that your units are 1/16º. E.g. 45º is 45*16 units.
Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction. Zero degrees is at the 3 o’clock position."
This means that 90*16 points at 12 o'clock (goes 90º counter-clockwise from 3 o'clock), and -90*16 points at 6 o'clock.
Of course the "zero" degrees only has sense for the start angle. The span angle states how much further does the arc go, and in which direction.
For example, to draw an arc from 3 o'clock to 12 o'clock, you'd do
painter.drawArc(rect, 0, 90*16)
*or*
painter.drawArc(rect, 90*16, -90*16)
But to draw an arc from 3 o'clock to 6 o'clock, you'd do
painter.drawArc(rect, 0, -90*16)
*or*
painter.drawArc(rect, -90*16, 90*16)
The arcs are not specified using center and radius, but rather using a bounding rectangle. If the arc was a full ellipse, it would be inscribed in the rectangle - the arcs are implicitly elliptical arcs.
So, given x and y centerpoint, and r for circular radius, the bounding rectangle is
rect = QRect(x-r, y-r, 2*r, 2*r)

Categories