How to count number of points inside a circle - python

I got this plot and I want to divide this plot into many different circles and need how many points in each circle.
I am trying to plot radius of the circle with how many number of points inside the circle.

Intuition:- Finding the distance between two points. (i.e sqrt((x2-x1)**2+(y2-y1)**2)) [Euclidean Formula]
If Distance>Radius than point is outside the circle
If Distance=Radius than point is on the circle
If Distance<Radius than point is inside the circle
Code:-
import math
# Lets say the circle points are x=2 y=3
x,y=2,3
# Radius of a Circle radius=4
radius=4
# Given points to check -:
lis=[(-1,1),(4,3),(5,4),(9,10),(1,2),(2,7)]
res=[]
for x1,y1 in lis: # x1,y1 points to check is it inside or not
if math.sqrt((x1-x)**2+(y1-y)**2)<radius: #Note use "<=radius" if you want point which is on the circle also
res.append((x1,y1))
print(res) #The points which are inside in the circle
Output:-
[(-1, 1), (4, 3), (5, 4), (1, 2)]

Related

How do I find the smallest surrounding rectangle of a set of 2D points in Shapely?

How do I find the msmallest surrounding rectangle (which is possibly rotated) of a set of 2D points in Shapely?
To create the smallest surrounding rectangle in Shapely, first construct a MultiPoint from a sequence of points then use the minimum_rotated_rectangle property (which is in the BaseGeometry class).
from shapely.geometry import MultiPoint, Polygon
points = [(0, 0), (2, 2), (10, 4), (5, 5), (8, 8)]
# create a minimum rotated rectangle containing all the points
polygon = MultiPoint(points).minimum_rotated_rectangle
print(polygon)
Output:
POLYGON ((2.9999999999999996 -2.9999999999999996, 10.999999999999998 4.999999999999998, ...
The example set of points are displayed below in red and the bounding box is in blue.
If want to create an envelope around the points which is the smallest rectangle (with sides parallel to the coordinate axes) containing all the points then can call the envelope property on the object.
polygon = MultiPoint(points).envelope

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.

how could I find the area of the intersection of line and contour

the blue pen is the contour
and the red pen is the straight line
how could I find the two areas of the intersection of line and contour
Now, I can get the contour area by
area = cv2.contourArea(np.array( [ [i] for i in blue_points ] ))
A simple but perhaps not the most efficient way would be to use cv.drawContours and cv.line to create two images: one with the contour of the blob and one with the contour of the line. Then cv.bitwise_and them together, and any point that is still positive will be points of intersection.
Shapely library makes it quick.
Assuming you have points of contour and line:
from shapely.geometry import Polygon, LineString
poly = Polygon([(5,5), (10,10), (10,0)])
a = LineString([(0, 0), (8, 8)])
print(a.intersects(poly))
There are options to speed up the code. Not checked.

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

Plotting heatmaps in python

I am using heatmap.py to plot a heatmap on python. I read on the doc (same page in the details section) that 'points' is "an iteratable list of tuples, where the contents are the
| x,y coordinates to plot. e.g., [(1, 1), (2, 2), (3, 3)]"
Therefore, we can specify the points (x,y) to color, but how is it possible to specify the intensity of each point (x,y)?
You do not directly specify the intensity, it is inferred from the number of points you place at any given coordinates. From the documentation:
The dot is placed into the output image for each input point at the translated output image coordinate. […] Dots are blended into the output image with an additive process: as points are placed on top of each other, they become darker. After all input points have been blended into the output image, the ouput image is colored based on the darkness of each pixel.
It seems you can make an area of the heat map more intense by adding more points the lie in that area.

Categories