Point with lat/long inside a circle - math

I know the formula for determining if a point(x, y) is inside a circle is (x - x_c)^2 + (y - y_c)^2 < radius^2 but what if my x and y are in degrees (lat/long)?

As mentioned by interjay, the solution is to calculate the distance between the point and the center of the circle and compare it with the radius. The code is available here:
Calculate distance between two latitude-longitude points? (Haversine formula)

Related

How to calculate the point lying on the outer circle following the tagent line of the inner circle

My question is about calculating points coordinates in 2D space.
I have two circles - outer and inner, that are centered between them (the inner is in the middle of the outer).
What I know:
-the two circles' radiuses (R1,R2)
-the 2D coordinates of a random point (x) in space always outside of the inner circle
What I want to find out:
-The 2D coordinates of the two points (y,z) that are lying on the outer circle following the two tangent lines from the random point (x)
Here is an illustration of what I need
Let's circles' center is coordinate origin (0,0) (shift other coordinates by true center ones), random point is P, point at big circle is Q, small radius is r, larger one is R.
We could build a system of equations for distance from center to tangent point and for intersection point, but it requires solving of quartic equation with rather long coefficients.
So at first find equation of tangent from point P to small circle with trigonometry:
Dist = Sqrt(px^2+py^2)
tan_angle = ArcSin(r / Dist)
rot_angle = ArcTan2(py, px)
ta1 = rot_angle - tan_angle
ta2 = rot_angle + tan_angle
and tangent points are
t1x = r * sin(ta1)
t1y = - r * cos(ta1)
t2x = - r * sin(ta2)
t2y = r * cos(ta2)
Now for both tangent points solve quadratic equation like
(px + s * (t1x - px))^2 + (py + s * (t1y - py))^2 = R^2
for unknown parameter s, get two solutions s1,s2 and find points of intersections
q11x = px + s1 * (t1x - px)
and so on
Note that solution consists of four points - two tangents, two intersection points for every tangent.

Calculate Point collision between a point of a given vector and the edge of a Circle

Lets say I have a point within a circle(not necessarily the origin) moving at a given vector how would I calculate the x and y coordinate of the point where it hits the edge of the circle.
Shift all coordinates by -cx, -cy. Now circle is centered at origin and has equation
x^2+y^2=R^2
Point coordinate (px, py), unit direction vector is (dx,dy). Equation of ray:
x = px + t * dx
y = py + t * dy
Substitute these variables into the circle equation, solve equation, find parameter t>0, then find intersection point (x,y), shift it back by (cx, cy).

How to find points on the circumference of a arc knowing a start point, an end point and the radius?

Please see the image below for a visual clue to my problem:
I have the coordinates for points 1 and 2. They were derived by a formula that uses the other information available (see question: How to calculate a point on a circle knowing the radius and center point).
What I need to do now (separately from the track construction) is plot the points in green between point 1 and 2.
What is the best way of doing so? My Maths skills are not the best I have to admit and I'm sure there's a really simple formula I just can't work out (from my research) which to use or how to implement.
In the notation of my answer to your linked question (i.e. x,y is the current location, fx,fy is the current 'forward vector', and lx,ly is the current 'left vector')
for (i=0; i<=10; i++)
{
sub_angle=(i/10)*deg2rad(22.5);
xi=x+285.206*(sin(sub_angle)*fx + (1-cos(sub_angle))*(-lx))
yi=y+285.206*(sin(sub_angle)*fy + (1-cos(sub_angle))*(-ly))
// now plot green point at (xi, yi)
}
would generate eleven green points equally spaced along the arc.
The equation of a circle with center (h,k) and radius r is
(x - h)² + (y - k)² = r² if that helps
check out this link for points http://www.analyzemath.com/Calculators/CircleInterCalc.html
The parametric equation for a circle is
x = cx + r * cos(a)
y = cy + r * sin(a)
Where r is the radius, cx,cy the origin, and a the angle from 0..2PI radians or 0..360 degrees.

Finding the distance between two circles

I'm trying to figure out how to get the distance from two circles relative to the corners of their square container boxes. I need some help with the maths here.
How can I work out the number of pixels for the line marked with a question mark?
Appreciate the help as always.
tldr: Calculate the distance between each circles center point, then subtract the radius' of each circle from that.
For the purpose of a demonstration, we will assume the following:
The 200px diameter (r1 = 100) circle is at the (x, y) coordinates of (0, 0), and
the 100px diameter (r2 = 50) circle is at (x, y) coordinates of (150, -150).
Given that the distance between their centers is:
To find the distance between their boundaries, we subtract the radius of each circle from the distance between their centers.
This leaves us with the equation:
sqrt((x2 − x1)^2 + (y2 − y1)^2) − (r2 + r1)
Inserting your values into the above gives:
sqrt((150 − 0)^2 + (-150 − 0)^2) − (100 + 50) = 62.132034356px
Do you have the middle point of each circles? If you do, first calculate the distance from the centre of circles.
distance² = center1² + center2²
Then, you will need to minus the radius of both circles. In your case, it will be 150 (100 + 50)
Let's see... each radius is half each side length, and subtracting the sum of the radii from the distance between the center gives you the amount that's left.
Hope that helps?
The algebraically simplified version of Daniel's answer is
(r1 + r2) * (sqrt(2) - 1)
= (s1 + s2) * (sqrt(2) - 1)/2
where r1,r2 are the two radii and s1,s2 are the two square sides. This is easily seen by looking at each square individually and noticing that the distance d1 from the circle/square center to the square corner is sqrt(2) * r, and the desired distance within that square is d1 - the circle radius r.

How to generate random shapes given a specified area.(R language).?

My question is this.. I am working on some clustering algorithms.. For this first i am experimenting with 2d shapes..
Given a particular area say 500sq units .. I need to generate random shapes for a particular area
say a Rect, Square, Triangle of 500 sq units.. etc .. Any suggestions on how i should go about this problem.. I am using R language..
It's fairly straightforward to do this for regular polygon.
The area of an n-sided regular polygon, with a circumscribed circle of radius R is
A = 1/2 nR^2 * sin((2pi)/n)
Therefore, knowing n and A you can easily find R
R = sqrt((2*A)/(n*sin((2pi)/n))
So, you can pick the center, go at distance R and generate n points at 2pi/n angle increments.
In R:
regular.poly <- function(nSides, area)
{
# Find the radius of the circumscribed circle
radius <- sqrt((2*area)/(nSides*sin((2*pi)/nSides)))
# I assume the center is at (0;0) and the first point lies at (0; radius)
points <- list(x=NULL, y=NULL)
angles <- (2*pi)/nSides * 1:nSides
points$x <- cos(angles) * radius
points$y <- sin(angles) * radius
return (points);
}
# Some examples
par(mfrow=c(3,3))
for (i in 3:11)
{
p <- regular.poly(i, 100)
plot(0, 0, "n", xlim=c(-10, 10), ylim=c(-10, 10), xlab="", ylab="", main=paste("n=", i))
polygon(p)
}
We can extrapolate to a generic convex polygon.
The area of a convex polygon can be found as:
A = 1/2 * [(x1*y2 + x2*y3 + ... + xn*y1) - (y1*x2 + y2*x3 + ... + yn*x1)]
We generate the polygon as above, but deviate angles and radii from those of the regular polygon.
We then scale the points to get the desired area.
convex.poly <- function(nSides, area)
{
# Find the radius of the circumscribed circle, and the angle of each point if this was a regular polygon
radius <- sqrt((2*area)/(nSides*sin((2*pi)/nSides)))
angle <- (2*pi)/nSides
# Randomize the radii/angles
radii <- rnorm(nSides, radius, radius/10)
angles <- rnorm(nSides, angle, angle/10) * 1:nSides
angles <- sort(angles)
points <- list(x=NULL, y=NULL)
points$x <- cos(angles) * radii
points$y <- sin(angles) * radii
# Find the area of the polygon
m <- matrix(unlist(points), ncol=2)
m <- rbind(m, m[1,])
current.area <- 0.5 * (sum(m[1:nSides,1]*m[2:(nSides+1),2]) - sum(m[1:nSides,2]*m[2:(nSides+1),1]))
points$x <- points$x * sqrt(area/current.area)
points$y <- points$y * sqrt(area/current.area)
return (points)
}
A random square of area 500m^2 is easy - its a square of side sqrt(500)m. Do you care about rotations? Then rotate it by runif(x,0,2*pi). Do you care about its location? Add an (x,y) offset computed from runif or whatever.
Rectangle? Given the length of any one pair of sides you only have the freedom to choose the length of the other two. How do you choose the length of the first pair of sides? Well, you might want to use runif() between some 'sensible' limits for your application. You could use rnorm() but that might give you negative lengths, so maybe rnorm-squared. Then once you've got that side, the other side length is 500/L. Rotate, translate, and add salt and pepper to taste.
For triangles, the area formula is half-base-times-height. So generate a base length - again, runif, rnorm etc etc - then choose another point giving the required height. Rotate, etc.
Summarily, a shape has a number of "degrees of freedom", and constraining the area to be fixed will limit at least one of those freedoms[1], so if you start building a shape with random numbers you'll come to a point where you have to put in a computed value.
[1] exactly one? I'm not sure - these aren't degrees of freedom in the statistical sense...
I would suggest coding a random walk of adjacent tiny squares, so that the aggregation of the tiny squares could be of arbitrary shape with known area.
http://en.wikipedia.org/wiki/File:Random_walk_in2D.png
It would be very tough to make a generic method.
But you could code up example for 3, 4, 5 sided objects.
Here is an example of a random triangle.(in C#)
class Triangle
{
double Angle1;
double Angle2;
//double angle3; 180 - angle1 - angle2;
double Base;
}
Triangle randomTriangle(double area){
//A = (base*hieght)/2.0;
double angle1 = *random number < 180*;
double angle2 = *random number < (180 - angle1)*;
*use trig to get height in terms of angles and base*
double base = (area*2.0)/height;
return new Triangle(){Angle1 = angle1, Angle2 = angle2, Base = base};
}

Resources