Python Shapes on grid - python

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 ;)

Related

How to find the centroid of multiple rectangles - Python

I have to find the exact centroid of multiple rectangles. The coordinates of each rectangle are as follows:
coord = (0.294792, 0.474537, 0.0989583, 0.347222) ## (xcenter, ycenter, width, height)
I have around 200 rectangles, how can I compute the centroid of them?
I already tried to implement it, but the code did not work well.
My code:
for i in range(len(xCenter)):
center = np.array((xCenter[i]+(Width[i]/2), yCenter[i]+(Height[i]/2)))
This is a somewhat vague question, but if you mean the centroid of all rectangles by area, then each center of a rectangle is weighted by the area of the rectangle. Think of it as the all the mass of the rectangle being compressed into the center, and then having to take the centroid of several weighted points. The formula for that would be the sum of 1 through n (assuming rectangles are numbered 1 to n) of Area(Rec(i)) * vec(center(i)) all divided by the total mass of the system (the sum of all the areas). If you are referring to the centroid of the area in general, ignoring rectangle overlap, that is a little more tricky. One thing you could do is for each rectangle, check it against all other rectangles, and if a pair of rectangles overlap, split them up into a set of non-overlapping rectangles and put them back into the set of rectangles. Once all rectangles are non-overlapping, find the centroid by mass.

what does extent mean for the function circle?

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.

Approximate 4 circles inside ellipse to get the radii

I need the approximate radii of the following ellipse.
The bottom/top and left/right radii should be the same nevertheless need to be checked. Which means 4 radii should be the result of my code. I did the following in paint, the green circle should give me the top radius and red the left (the right and bottom one aren't drawn here).
The idea I'm working on is to crop the image (left/right/top/bottom side) and approximate circles to the cropped images. With the cv2.findContours-feature some white pixels get recognized as highlighted here.
Is there a way to approximate my drawn red circle from above with these given coordinates? The problems I've seen on the internet are all with a given center point or angle which I don't have. Is there a cv2 function that draws circles with only some given coordinates or something similar?
Use this function : cv2.fitEllipse(points) and pass contour points -Ziri
Yes this did the trick. I got the radii after your function with:
(x, y), radius = cv2.minEnclosingCircle(i)

Rotate a rectangle until it hits a triangle, and determine the point of intersection

This problem is in 3D space.
There is a rectangle, defined by 4 vertices. We rotate it around one of its sides.
There is a triangle, defined by 3 vertices.
After a full 360 degree rotation, will the rectangle ever intersect/touch the triangle?
If so, what is the angle of rotation at which intersection first occurs? And what is the point of this first intersection?
After thinking about this for a while, it seems like there are 3 main cases:
triangle vertex touches rectangle surface
triangle surface touches rectangle vertex
triangle edge touches rectangle edge
And there are 2 unlikely cases where the two are perpendicular when the intersect:
rectangle edge hits triangle surface
rectangle surface hits triangle edge
However identifying these cases hasn't really gotten me closer to a solution. I'm hoping someone can point me in the right direction for how to solve this problem. I want to solve it fast for a small number of rectangles x a large number of triangles.
Context: the larger problem I'm trying to solve is I want to wrap a rectangle around a closed polygonal mesh. I wish to do this step by step by rotating the rectangle until it intersects, then rotating the remaining rectangle around the intersection point, etc.
When you rotate a rectangle around one of its sides, you get a cylinder. Intersect each of the lines with the cylinder. The position of the intersection points gives you the rotation angles. Since this doesn't catch the case where the triangle is completely contained within the cylinder, test whether the vertices' distance to the cylinder's axis is smaller than the cylinder's radius, too.
Say your rectangle has the vertices A to D. You want to rotate around the side AB. The radius of your cylinder is then r = |AD|.
First, transform the coordinates so that the rectangle is placed with the side that you want to rotate about along the z axis and the adjacent side along the x axis.
A′ = {M} · A = {0, 0, 0}
B′ = {M} · B = {0, 0, |AB|}
C′ = {M} · C = {r, 0, 0}
Apply the same transformation {M} to the vertices of the triangle.
Now find the intersections of all three sides of the triangle with the cylinder. Because the cylinder is aligned to the z axis, the problem can be separated into two subproblems: (1) Find any intersections with the top and bottom surfaces a z == 0 and z == |AB|. (2) Find the intersections with the "coat" of the cylinder; this is the intersection of a line with a circle in the xy plane.
You can then calculate the rotation angles with the tangent function of the y and x coordinates of these points as atan2(y, x).
If you need the coordinates of the intersection points in the original coordinates, don't forget to undo the transformation.

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)

Categories