Circle Distance Question - python

This is my homework question:
Expand on your Circle class by adding a method called exactly dist() which takes the x and y values of a point, and returns the distance of the point in coordinate space from the outside of the circle (or zero if the point is on or inside the circle).
The following code:
myCircle = Circle(1,1,1)
print myCircle.dist(3,4)
Should print an output of approximately:
2.6055512754639891
However I cant understand the question. What does it mean to return the point in coordinate space from the outside of the circle? Can you please explain it?

Find the distance between the center of the circle and the given point. Subtract the radius from the distance. Negative values are inside the circle.

You can easily check if the point is inside the circle on not. Once you've done that, and determined it is outside, you need to find a line normal to the circle and passing through the point. The length of the line from the circumference of the circle gives you the answer.
Hint: Any line passing through the center of the circle is normal to it.
So you have the point (x,y), radius r and center (x0,y0). I think you have enough information here to solve the problem :)

It probably means the distance to the closest point on the circle, so its a min-max problem wherre you are minimizing the distance of the point to circumfurance

>>> from math import hypot
>>> hypot(3-1,4-1) # How far is the point from the centre of the circle?
3.6055512754639891
>>> hypot(3-1,4-1)-1 # Now subtract the radius
2.6055512754639891
>>>

Related

Latitude and Longitude Lying in a Rectangular Area

While 2 corner points were given of an area, rectangular in this case, I was trying to find whether a custom given coordinates are going to be within that rectangle or not. However, the magic is the Earth being a sphere. I believe, even though it looks like a rectangle, it is really not as it bends down from the sides. So, my question is that how to find whether a given Latitude/Longitude is lying within another created Rectangle or not?
I have written a custom Python script to get the lower & upper corners of the rectangle in latitude and longitude already, which returns values as following:
(['35.805949', '25.552255'], ['42.278201', '44.817695'])
What is the algorithm lying behind this idea?
The projection geometry simply denies the earth's curvature. This is trivial plan geometry. Let you corners be (w, n) and (e, s). To find whether (x, y) is in the rectangle, use a trivial bounds check:
if w <= x <= e and
s <= y <= n:
You may need to adjust the comparisons if you use some other coordinate system; I'm using the standard Cartesian mapping.

Determine the closest point

Math dummy question:
Say I have a "root" coordinate (x0,y0) and a list of other coordinates ((x1,y2),(x2,y2),...(xn,yn))
Is there a computationally better method to find the nearest point to (x0,y0) than to scan through and get all the distances to (x0,y0) using the Pythagorean theorem?
Thank you
Simpler than doing 2 multipltication?
I don't think so.
Just be aware that you do not need to compute the square root to compare which point is closer (because sqrt is a monotonoic incleasing function).
So compute (x0-xi)^2+(y0-yi)^2 for each i in {1,..,n}
and take the one with the smallest value. That's the nearest point.

Points in which polygon

I have several (order of M) points [(x0,y0),...,(xn,yn)]. I also have limited number of hexagons. I want to find each point falls in which hexagon. Using shapely this can be done for 1 point at a time so the loop below does the job. But is there any other way to do it faster?
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
zones = np.zeros(n)-1
for j,p in enumerate(points):
point = Point(p)
for i in range(len(poly_hex)): #poly_hex = list of hexagonal polygones
polygon = Polygon(poly_hex[i])
if polygon.contains(point):
zones[j] = int(i)
break
Some ideas to speed things up:
Don't convert your hexagons to polygon = Polygon(poly_hex[i]) at each step of the inner loop. Calculate them once before the loop and store them in a list.
As the surface of a hexagon is close to a circle, also make a list of just the centre and the radius of the hexagons. Inside the loop, first test the distance between the point p and the centre of each hexagon, comparing to the radius. Only in case p is less than the radius to the centre, do an explicit test to see whether it is inside the hexagon.
If this still isn't fast enough, and depending on how small, and how far apart the hexagons are relative to your points, you could speed things up further with a quadtree or a grid. But this is less trivial. If most of the points are outside all hexagons, you could first test whether p is inside the convex hull of the hexagons.
Some remarks:
I don't understand your zone[j] = int(i). You convert i to an integer, while it already is an integer. Also, the way you store this, means a point can only be inside maximum one hexagon. Therefore, you can stop the loop as soon as you encountered such a hexagon. (There is also the rare case where a point is just on the edge between two hexagon. This might assign it to the first hexagon encountered. Or, due to rounding errors, even might have it outside both hexagons.)
If you want to save the zones as integers, create it as: zones = np.zeros(n, dtype=int)-1.
If your hexagons form a regular hexagonal grid, you can find the exact hexagon simply from the x and y coordinates without the need for in-polygon testing.

Matplotlib Axes3d, line intersect with 2d object

I am trying to do this but in 3d and using a 2d circle instead of a box.
I have a line starting between the two points [ (0,0,0), (3,4,5) ] and I want to see if it intersects through
circle = Circle((2, 1), 0.5)
ax.add_patch(circle)
art3d.pathpatch_2d_to_3d(circle, z=1, zdir="x")
Is it possible to test for a path intersect on a 2d object plotted on 3d axis? From the linked example above, I want to do path.intersects_circle where I define a circle as:
I have had a look through the Bbox documentation and it seems that I can't use this method for a circle?
This sounds more like an algebraic problem than related to matplotlib.
This is how I understand your question:
you have a circle at (x=2,y=1) with a radius of r=0.5
this circle is located in a plane at a constant z=1
1.) You need to determine where your vector pierces the plane which is parallel to the x,y-plane and at z=1. For the vector you specify in your question this intersection is at:
x = 3./(2.**0.5)
y = 4./(2.**0.5)
z = 1.
2.) You need to determine if this intersection falls into the part of the plane covered by the circle. The maximum y-coordinate your circle reaches is 1.5 - the y-coordinate of the intersection is already larger. Hence your straight line does not pierce the circle.
All this being said, I would recommend implementing an algebraic check based on the intersection with the plane and determining if this intersection is part of the circle. And only then using matplotlib.

Radius of a cube

This may be a silly question, but I basically need to find the radius of a cube in MAya.
Or another explanation would be I need to find the absolute center point, to the axis of choice example Y, so it would find out the radius of the Y direction if specified, or the distance from the center point to the furthest X direction, this would be the "radius".
Would love the help!
By analogy in 2 dimensions - or think of this as the center slice of the sphere in the cube.

Categories