I have the following problem, I am trying to find the following distances (F1 and F2):
This is what I have as of now:
def FindArrow(self, X1, Y1, X2, Y2, X3, Y3):
self.X1 = float(X1)
self.Y1 = float(Y1)
self.X2 = float(X2)
self.Y2 = float(Y2)
self.X3 = float(X3)
self.Y3 = float(Y3)
#center coords of the circle
self.Xc = None
self.Yc = None
#radius
self.R = None
#F1 and F2
self.FAB = None
self.FBC = None
#check if the coordinates are collinear
invalide = self.X1 * (self.Y2 - self.Y3) + self.X2 * (self.Y3 - self.Y1) + self.X3 * (self.Y1 - self.Y2)
if (invalide == 0):
return
#get the coords of the circle's center
s = (0.5 * ((self.X2 - self.X3)*(self.X1 - self.X3) - (self.Y2 - self.Y3) * (self.Y3 - self.Y1))) / invalide
self.Xc = 0.5 * (self.X1 + self.X2) + s * (self.Y2 - self.Y1)
self.Yc = 0.5 * (self.Y1 + self.Y2) + s * (self.X1 - self.X2)
#get the radius
self.R = math.sqrt(math.pow(self.Xc - self.X1, 2) + math.pow(self.Yc - self.Y1, 2))
Until here everything seems to work, now what would be the next steps to get F1 and F2 ?
EDIT:
Both of the answers (Tomer and inerjay) seem to work for the first image, but I have tried the following image and it does not work anymore :
If you have the circle's radius, you can use Pythagoras' Theorem to get the distance from the circle's center to the segment P2-P3 (look at the triangle with its vertices at: P2, the circle's center, and the middle of P2-P3). Then subtract that from the radius to get F2. Same method to get F1.
Edit: If you call x the distance between the center and the segment P2-P3, then F2 is either R-x (as in the first image), or R+x (as in the second image). It depends on how you define F1 and F2.
If you define F1 and F2 so that they always go outward from the triangle P1-P2-P3 (this fits both your examples), then F2 = R-x if the circle's center is on the same side of P2-P3 as P1, and F2 = R+x if P1 and the circle's center are on opposite sides of P2-P3.
If I understand correctly, the segments F1,F2 point towards the cirlce's center. Mark the circle's center O, and build the triangle whose vertices are (P1,P2,O). You know all the lengths of the edges, so you can calculate the triangle's area S (see here). And since you know the area, you also know the length of the triangle's height, which is the continuation of F1 to O:
S = 0.5*dist(P1,P2)*height
Then F1 = radius - height, and same goes for F2.
Related
In a game, an area is defined by triangles that never overlap, and characters are defined by circles.
How can I know whether the full character's collision circle is contained within these triangles?
Example image:
Here, the red parts are outside triangles, so the circle isn't contained within them. Is there an algorithm that can detect this?
I've only came up with "non-perfect" solutions, like sampling points at the border of the circle, then testing if each is inside a triangle.
So basically, the triangles form a domain with polygonal boundary and you want to check if a disk, defined by a center point and a radius is contained inside the domain. So if you start with the triangles, you have to find a way to extract the polygonal boundary of your domain and represent it as a 2D array (matrix) of shape n rows and two columns so that every row is the two coordinates of a vertex point of the polygonal boundary line and the points are ordered so that they are consecutive order along the boundary in a counterclockwise position, i.e. when you walk in a direction from point of index i to the next point i+1 the domain stays on your left. For example, here is the representation of a polygonal boundary of a domain like yours:
a = 4/math.sqrt(3)
Pgon = np.array([[0,0],
[a,0],
[2*a,-1],
[2*a+4,0],
[2*a+4,4],
[2*a,4],
[2*a,2],
[a,1],
[a,4],
[0,0]])
Observe that the first and the last points are the same.
In such a scenario, maybe you can try the following algorithm:
import numpy as np
import math
def angle_and_dist(p1, p2, o):
p12 = p2 - p1
op1 = p1 - o
op2 = p2 - o
norm_p12 = math.sqrt(p12[0]**2 + p12[1]**2)
norm_op1 = math.sqrt(op1[0]**2 + op1[1]**2)
norm_op2 = math.sqrt(op2[0]**2 + op2[1]**2)
p12_perp = np.array([ - p12[1], p12[0] ])
h = - op1.dot(p12_perp)
theta12 = op1.dot(op2) / (norm_op1*norm_op2)
theta12 = math.acos( theta12 )
if h < 0:
theta12 = - theta12
if op1.dot(p12) > 0:
return theta12, norm_op1
elif op2.dot(p12) < 0:
return theta12, norm_op2
else:
return theta12, h/norm_p12
def is_in_polygon(p, disk):
o, r = disk
n_p = len(p)-1
index_o = 0
h_min = 400
for i in range(n_p):
theta, h = angle_and_dist(p[i,:], p[i+1,:], o)
index_o = index_o + theta
if 0 <= h and h < h_min:
h_min = h
if theta <= math.pi/100:
return 'center of disc is not inside polygon'
elif theta > math.pi/100:
if h_min > r:
return 'disc is inside polygon'
else:
return 'center of disc is inside polygon but disc is not'
a = 4/math.sqrt(3)
Pgon = np.array([[0,0],
[a,0],
[2*a,-1],
[2*a+4,0],
[2*a+4,4],
[2*a,4],
[2*a,2],
[a,1],
[a,4],
[0,0]])
# A test example:
#disc = (np.array([3*a/4, 2]), a/4-0.001)
disc = (np.array([3*a/4, 2]), math.sqrt(3)*a/8 - 0.0001)
print(is_in_polygon(Pgon, disc))
I try to rotate and translate an equilateral triangle in 3D until his vertices reach some coordinates.
The vertices coordinates F,G,H and F',G',H' are known :
I was able to find the new centroid c' coordinates like this :
c'.x = ( F'.x + G'.x + H'.x ) / 3
c'.y = ( F'.y + G'.y + H'.y ) / 3
c'.z = ( F'.z + G'.z + H'.z ) / 3
So no problem to translate the triangle. But I can't find a way to calculate the rotations needed to put F'G'H' triangle in the right position...
I have to know by how much the triangle F'G'H' has to be rotated in degrees, around each axis (x,y,z), knowing that the rotations of the initial triangle are 0°.
By rotation for each axis, I'm talking about this:
Any ideas?
trick is to find the normal vectors of the triangles using cross product b4 and after rotations
v1 = (F.x - G.x, F.y - G.y, F.z - G.z)
v2 = (F.x - H.x, F.y - H.y, F.z - H.z)
n = cross_prod(v1, v2) # see http://en.wikipedia.org/wiki/Cross_product
n = n / norm(n) # normalize to unit vector
v'1 = (F'.x - G'.x, F'.y - G'.y, F'.z - G'.z)
v'2 = (F'.x - H'.x, F'.y - H'.y, F'.z - H'.z)
n' = cross_prod(v'1, v'2)
n' = n' / norm(n')
rot = arc_cos(n.x * n'.x + n.y * n'.y + n.z * n'.z)
I am trying to create a binary tree from a lot of segments in 3d space sharing the same origin.
When merging two segments I want to have a specific angle between the lines to the child nodes.
The following image illustrates my problem. C shows the position of the parent node and A and B the child positions. N is the average vector of the vectors from C to A and C to B.
With a given angle, how can I determine point P?
Thanks for any help
P = C + t * ((A + B)/2 - C) t is unknown parameter
PA = A - P PA vector
PB = B - P PB vector
Tan(Fi) = (PA x PB) / (PA * PB) (cross product in the nominator, scalar product in the denominator)
Tan(Fi) * (PA.x*PB.x + PA.y*PB.y) = (PA.x*PB.y - PA.y*PB.x)
this is quadratic equation for t, after solving we will get two (for non-degenerate cases) possible positions of P point (the second one lies at other side of AB line)
Addition:
Let's ax = A.x - A point X-coordinate and so on,
abcx = (ax+bx)/2-cx, abcy = (ay+by)/2-cy
pax = ax-cx - t*abcx, pay = ay-cy - t*abcy
pbx = bx-cx - t*abcx, pby = by-cy - t*abcy
ff = Tan(Fi) , then
ff*(pax*pbx+pay*pby)-pax*pby+pay*pbx=0
ff*((ax-cx - t*abcx)*(bx-cx - t*abcx)+(ay-cy - t*abcy)*(by-cy - t*abcy)) -
- (ax-cx - t*abcx)*(by-cy - t*abcy) + (ay-cy - t*abcy)*(bx-cx - t*abcx) =
t^2 *(ff*(abcx^2+abcy^2)) +
t * (-2*ff*(abcx^2+abcy^2) + abcx*(by-ay) + abcy*(ax-bx) ) +
(ff*((ax-cx)*(bx-cx)+(ay-cy)*(by-cy)) - (ax-cx)*(by-cy)+(bx-cx)*(ay-cy)) =0
This is quadratic equation AA*t^2 + BB*t + CC = 0 with coefficients
AA = ff*(abcx^2+abcy^2)
BB = -2*ff*(abcx^2+abcy^2) + abcx*(by-ay) + abcy*(ax-bx)
CC = ff*((ax-cx)*(bx-cx)+(ay-cy)*(by-cy)) - (ax-cx)*(by-cy)+(bx-cx)*(ay-cy)
P.S. My answer is for 2d-case!
For 3d: It is probably simpler to use scalar product only (with vector lengths)
Cos(Fi) = (PA * PB) / (|PA| * |PB|)
Another solution could be using binary search on the vector N, whether P is close to C then the angle will be smaller and whether P is far from C then the angle will be bigger, being it suitable for a binary search.
I'm currently working on a game in LBP2 that has modify the way a controller gives input. This question:
How can I convert coordinates on a square to coordinates on a circle?
Has helped me quite a lot with what I am doing, but I do have one problem. I need the inverse function of the one they give. They go from square -> circle, and I've tried searching all over for how to map a circle to a square.
The function given in the previous question is:
xCircle = xSquare * sqrt(1 - 0.5*ySquare^2)
yCircle = ySquare * sqrt(1 - 0.5*xSquare^2)
From Mapping a Square to a Circle
My question is given xCircle and yCircle... how do I find xSquare and ySquare?
I've tried all of the algebra I know, filled up two pages of notes, tried to get wolfram alpha to get the inverse functions, but this problem is beyond my abilities.
Thank you for taking a look.
x = ½ √( 2 + u² - v² + 2u√2 ) - ½ √( 2 + u² - v² - 2u√2 )
y = ½ √( 2 - u² + v² + 2v√2 ) - ½ √( 2 - u² + v² - 2v√2 )
Note on notation: I'm using x = xSquare , y = ySquare, u = xCircle and v = yCircle;
i.e. (u,v) are circular disc coordinates and (x,y) are square coordinates.
For a C++ implementation of the equations, go to
http://squircular.blogspot.com/2015/09/mapping-circle-to-square.html
See http://squircular.blogspot.com
for more example images.
Also, see http://arxiv.org/abs/1509.06344 for the proof/derivation
This mapping is the inverse of
u = x √( 1 - ½ y² )
v = y √( 1 - ½ x² )
P.S. The mapping is not unique. There are other mappings out there. The picture below illustrates the non-uniqueness of the mapping.
if you have xCircle and yCircle that means that you're on a circle with radius R = sqrt(xCircle^2 + yCircle^2). Now you need to extend that circle to a square with half-side = R,
if (xCircle < yCircle)
ySquare = R, xSquare = xCircle * R/yCircle
else
xSquare = R, ySquare = yCircle * R/xCircle
this is for the first quadrant, for others you need some trivial tweaking with the signs
There are many ways you could do this; here's one simple way.
Imagine a circle of radius R centred on the origin, and a square of side 2R centred on the origin, we want to map all of the points within and on the boundary of the circle (with coordinates (x,y)) to points within and on the boundary of the square. Note that we can also describe points within the circle using polar coordinates (r, ø) (that's supposed to be a phi), where
x = r cos ø,
y = r sin ø
(ie r^2 = x^2 + y^2 and r <= 1). Then imagine other coordinates x' = a(ø) x = a(ø) r cos ø, and y' = a(ø) y (ie, we decide that a won't depend on r).
In order to map the boundary of the circle (r = 1) to the boundary of the square (x' = R), we must have, for ø < 45deg, x' = a(ø) R cos ø = R, so we must have a(ø) = 1/cos ø. Similarly, for 45 < ø < 90 we must have the boundary of the circle map to y' = R, giving a(ø) = 1/sin ø in that region. Continuing round the circle, we see that a(ø) must always be positive, so the final mapping from the circle to the square is
x' = a(ø) x,
y' = a(ø) y
where
ø = |arctan y/x| = arctan |y/x|
and
a(ø) = 1/cos ø, when ø <= 45 deg (ie, when x < y), and
a(ø) = 1/sin ø, when ø > 45 deg.
That immediately gives you the mapping in the other direction. If you have coordinates (x', y') on the square (where x' <= R and y' <= R), then
x = x'/a(ø)
y = y'/a(ø)
with a(ø) as above.
A much simpler mapping, though, is to calculate the (r, ø) for the desired position on the circle, and map that to x' = r and y' = ø. That also maps every point in the circle into a rectangle, and vice versa, and might have better properties, depending on what you want to do.
So that's the real question: what is it you're actually aiming to do here?
I was implementing the solution above but the results are not satisfiying.
The square coordinates are not exact.
Here is a simple counter-example:
Consider the point (x,y)=(0.75, 1) on the square.
We map it to the circle with (u,v)=(0.53, 0.85) on the circle.
Applying the expression above we get the new square coordinates
(x',y')=(u/v,r)=(0.625543242, 1) with r=(u^2+v^2)^(1/2).
This point is close but not the expected precise solution.
I solved a root finding problem in order to get the inverse expression of the mapping from square to circle like above.
you need to solve the system equations like above:
I) u = x*(1-y^2/2)^(1/2)
II) v = y*(1-x^2/2)^(1/2)
One ends up with 8 root points as solution. One of the roots I implemented into Excel-VBA which I present here below and it works very fine.
' given the circle coordinates (u,v) caluclates the x coordinate on the square
Function circ2sqrX(u As Double, v As Double) As Double
Dim r As Double, signX As Double, u2 As Double, v2 As Double, uuvv As Double, temp1 As Double
u2 = u * u
v2 = v * v
r = Sqr(u2 + v2)
signX = 1
If v = 0 Or u = 0 Then
circ2sqrX = u
Exit Function
End If
If u < 0 Then
signX = -1
End If
If Abs(u) = Abs(v) And r = 1 Then
circ2sqrX = signX
Exit Function
End If
uuvv = (u2 - v2) * (u2 - v2) / 4
temp1 = 2 * Sqr(uuvv - u2 - v2 + 1)
circ2sqrX = -((temp1 - u2 + v2 - 2) * Sqr(temp1 + u2 - v2 + 2)) / (4 * u)
End Function
' given the circle coordinates (u,v) caluclates the y coordinate on the square
' make use of symetrie property
Function circ2sqrY(u As Double, v As Double) As Double
circ2sqrY=circ2sqrX(v,u)
End Function
I'm using OpenCV to fit a line from a set of points using cvFitLine()
cvFitLine() returns a normalized vector that is co-linear to the line and a point on the line.
See details here
Using this information how can I get the equation of a line so that I can draw the line?
If cvFitLine() returns normalized vector (vx,vy) and point (x0,y0), then the equation of the line is
(x,y) = (x0,y0) + t*(vx,vy)
where t runs from −∞ to +∞.
This is what you asked for, but probably isn't immediately helpful in drawing the line. You would want to clip it either to the screen boundaries, or perhaps the bounding box of the the original set of points. To clip a line to a rectangle, just solve for values of t where the line crosses the boundary of the rectangle.
Just draw a big line instead of solving for the boundaries. eg:
cv.Line(img, (x0-m*vx[0], y0-m*vy[0]), (x0+m*vx[0], y0+m*vy[0]), (0,0,0))
will do it for example.. for m large enough :)
This just spells out #brainjam's answer in python for any passers by.
The formula for a line using a unit vector (vx, vy) and some point on the line (x0, y0) is:
(x, y) = (x0, y0) + t*(vx, vy)
The return from cv2.fitLine() is:
np.array([vx, vy, x0, y0])
In the example case, I have a line spanning the height of my image, so I want to find the t0 and t1 that intersect with y=0 and y=img.shape[0] (the top/bottom boundaries).
# get the fitLine for your set of points in the array, `line`
fit_line = cv2.fitLine(line, cv2.DIST_L2, 0, 0.01, 0.01)
# compute t0 for y=0 and t1 for y=img.shape[0]: (y-y0)/vy
t0 = (0-fit_line[3])/fit_line[1]
t1 = (img.shape[0]-fit_line[3])/fit_line[1]
# plug into the line formula to find the two endpoints, p0 and p1
# to plot, we need pixel locations so convert to int
p0 = (fit_line[2:4] + (t0 * fit_line[0:2])).astype(np.uint32)
p1 = (fit_line[2:4] + (t1 * fit_line[0:2])).astype(np.uint32)
# draw the line. For my version of opencv, it wants tuples so we
# flatten the arrays and convert
# args: cv2.line(image, p0, p1, color, thickness)
cv2.line(img, tuple(p0.ravel()), tuple(p1.ravel()), (0, 255, 0), 10)
I used a strategy similar to Karpathy up there but used an extra function. As you can see, I'm using cvClipLine to trim the line to the size of the image, which is unnecessary but does add a little niceness.
Also the multiplier here is defined as theMult = max(img->height,img->width) so we dont get numbers that might one day overflow or something.
void drawLine(IplImage * img, float line[4], int thickness,CvScalar color)
{
double theMult = max(img->height,img->width);
// calculate start point
CvPoint startPoint;
startPoint.x = line[2]- theMult*line[0];// x0
startPoint.y = line[3] - theMult*line[1];// y0
// calculate end point
CvPoint endPoint;
endPoint.x = line[2]+ theMult*line[0];//x[1]
endPoint.y = line[3] + theMult*line[1];//y[1]
// draw overlay of bottom lines on image
cvClipLine(cvGetSize(img), &startPoint, &endPoint);
cvLine(img, startPoint, endPoint, color, thickness, 8, 0);
}
Adding to #brainjam answer:
To clip to the bounding box of original set of points:
// std::vector<Point2i> points = ...
//lineParams: [vx,vy, x0,y0]: (normalized vector, point on our contour)
Vec4f lineParams; fitLine(points, lineParams, CV_DIST_L2, 0, 0.01, 0.01);
// derive the bounding xs of points
decltype(points)::iterator minXP, maxXP;
std::tie(minXP, maxXP) = std::minmax_element(points.begin(), points.end(), [](const Point2i& p1, const Point2i& p2){ return p1.x < p2.x; });
// derive y coords of fitted line
float m = lineParams[1] / lineParams[0];
int y1 = ((minXP->x - lineParams[2]) * m) + lineParams[3];
int y2 = ((maxXP->x - lineParams[2]) * m) + lineParams[3];
line(clearTarget, Point(minXP->x, y1), Point(maxXP->x, y2), Scalar(255, 255, 255), 2);
To clip to the entire image boundaries substitute minXP->x to 0 and maxXP->x to image.cols - 1, which was originally answered in https://stackoverflow.com/a/14192660/2380455
we use a " Vec4f fitedLine;" for fitted Line
in fitLine we have 4 parameters
if we consider Line relation az bellow:
Y - Y0 = M (X - X0)
we have
Y0 = FitedLine[3];
X0 = FitedLine[2];
m = FitedLine[1]/FitedLine[0];
so we have a Line equation we can find other points on it.