How do I find the intersection of two lines in maple when plotted as follows:
a:=line([1,-1,-1],[0,0,1]):
b:=line([1,1,1],[0,-1,0]):
I attempted to use the intersection command but it returned this:
intersection(CURVES([[1., -1., -1.], [0., 0., 1.]]), CURVES([[1., 1., 1.], [0., -1., 0.]]))
Thanks very much for any help
line's first argument is the name of the line you are symbolically defining.
Instead of using
a:=line([1,-1,-1],[0,0,1]):
b:=line([1,1,1],[0,-1,0]):
Try the following instead:
with(geom3d):
point(p1,[1,-1,-1]):
point(p2,[0,0,1]):
point(p3,[1,1,1]):
point(p4,[0,-1,0]):
line(l1,[p1, p2]):
line(l2,[p3, p4]):
intersection(P,l1,l2):
coordinates(P)
See the help on intersection and line for more detail.
Related
I have a graph with nodes, edges, and edge features. I want to make a model with SAGEConv but SAGEConv only look at node features. So I thought of making node feature by aggregating edge features of each node and I found out about NNConv, where they're able to do that. The problem is I'm not sure how to use NNConv, especially what do I do with edge_func that is required as a parameter for NNConv?
Let say I have 3 nodes and 4 edges, each edge has 2 features, and I want the each nodes to have 2 features as well. I defined edge_func to be
edge_func = nn.Sequential(nn.Linear(2, 2), nn.ReLU(), nn.Linear(2, 4))
and edge_feature to be
tensor([[2., 3.],
[3., 1.],
[0., 4.],
[1., 1.]])
and build a DGLGraph with that. However after computing the layer
NNConv(in_feats=2, out_feats=2, edge_nn, aggregator_type='max')
in the forward function
feature = self.layer(self.graph, feature, edge_feature)
returns feature (it was originally tensor of zeros) as
tensor([[-inf, -inf],
[0., 0.],
[0., 0.]], grad_fn=<AddBackward0>)
What am I doing wrong?
Im writing a basic script to find the min distance between f(x):=log(x)-x and the origin. I would like to be able to plot the point closest to the origin over the top of a plot of f(x), but i can not figure out how to plot a single point.
Here is what i have written. Any ideas?
f(x):=log(x)-x;
d(x):=sqrt(x^2+f(x)^2)$
find_root(diff(d(x),x),x,0.01,5)$
a:%;
f(a);
print("min distance from f(x) to (0,0)")$
d(a);
print("passes second derivative test if next value greater than zero")$
g(x):=''(diff(d(x),x,2))$
g(a);
wxplot2d([f(x)], [x,.01,5], [y,-6,0])$
Use the discrete option as a second curve, and then use points in the style option.
Replacing your last line with
wxplot2d([f(x), [discrete, [a], [f(a)]]], [x,.01,5], [y,-6,0],
[style, lines, points],
[legend, "log(x)-x", "closest point to origin"],
[point_type, circle],
[gnuplot_preamble, "set key bottom"])$
gives you this:
There are two circles: a centered at point A, and circle b (center at B). What is the equation to calculate 2D position of all or none tangent circles possible. Main constraint is, that radius is the same for all the circles. As far as I know, there should be either no solution (figure 2), or 2 solutions (figure 1). How to find out if there are solutions, and also position of centers of those solutions (C and D).
Figure 1: 2 solutions should be possible here
Figure 2: No solutions!
Update (solution):
1) Calculate distance from A to B -> |AB|:
2) Checks whether a solution exist, it exist only if:
3) If it exist, calculate half-point between points A and B:
4) Create normalized perpendicular vector to line segment AB:
5) Calculate distance from this H point to C point -> |HC|:
6) Finally calculate point C along the (HC) starting at X at distance |HC|:
I suppose this question should migrate to a more math related site.
Try to imagine where these two tangent circles go when the circles a and b get further and further apart. They get closer to the line AB. Once the AB segment equals 4r these two tangent circles will overlap. From now on, once circles a and b get further apart, there's no tangent circles whatsoever.
If you want to calculate the position of these circles, just assume that the distance between the centers is always 2r:
You should get two, one or none solutions for xC and yC, which will be the centers of your tangent circles. I hope I haven't messed something up.
Solutions
Provided you do know there are solutions ( just check if d(A,B) <= 4r ), these are the coordinates of your two circles:
http://pastebin.com/LeW7Ws98
A little scary, eh? But it's working. There are the following variables:
x_A, y_A - the coordinates of the circle A,
x_B, y_B - the coordinates of the circle B,
r - the radius.
I've checked the solutions with the values from one of my comments below. I think that you can copy these solutions and inject them into your code straight away (provided there's a sqrt function) and get the results after declaring some variables.
These solutions are loosely derived from the Save's proposition but I couldn't comment below his answer - I've got less than 50 reputation points, duh ... ( thanks SO! You're the man! ). However I'm pretty sure they should be valid for my system anyways. Cheers
A solution exists iff d(A,B) = sqrt(2)*2*r
To find the center of the solution circles, that will let you draw the circonferences, you can intersect the circle with center (x_m,y_m), that is the medium point of the segment AB, of radius sqrt(2)*r, with the line perpendicular to AB and passing from (x_m,y_m)
This should give you all the needed information to check if a solution exixsts, and if it does, to draw it.
I've seen many postings here on stackoverflow, which are discussing this topic. I took a solution from stackoverflow, but I couldn't find the posting. It was to say: If two lines are intersecting, then the crossproduct produces for the left and the right side two different results. One positive and one negative. Otherwise both have the same sign. So far it is okay.
The used formula is, where AB is one line and CD another.
dotproductleft = (B.x-A.x)(C.y-B.y)-(B.y-A.y)(C.x-B.x)
dotproductright = (B.x-A.x)(D.y-B.y)-(B.y-A.y)(D.x-B.x)
If I calculate this for the following given GPS coordinates I get an intersection, in a case where no intersection is possible.
A: x: 15.4433917 y: 47.0697272
B: x: 15.4433661 y: 47.0697722
C: x: 15.4434363 y: 47.0696776
D: x: 15.442966 y: 47.0700098
If I draw it on Google Maps Engine, I can see that there definitely no intersection between these lines.
The results for dotproductleft: -7.372399999828474E-10 and for dotproductright: 1.1921940000328E-8
It is working for other examples very well, but here I've seen that it is not working right. But I couldn't spot any error in the formula.
The lines which contain any line segment (e.g. AB and CD) will always intersect (unless they are parallel).
You want to check if the line segments intersect. This is easy to do by finding the point of intersection and checking if it lies on both line segments.
So, using your example, the two lines (on which line segments AB and CD lie on) intersect at P = (15.4434,47.0697). Now, if P lies on AB then the dot product of PA and PB is negative (since the angle between PA and PB is 180° and cos(180°)=-1).
If you compute the dot product of PA and PB, you will get a positive number (thus it does not lie on AB). However, the dot product of PC and PD gives you a negative number (thus it lies on CD).
Thus, you can check if the line segments intersect if the aforementioned dot products (with the point of intersection) are negative.
I'm looking for an algorithm to find the common intersection points between 3 spheres.
Baring a complete algorithm, a thorough/detailed description of the math would be greatly helpful.
This is the only helpful resource I have found so far:
http://mathforum.org/library/drmath/view/63138.html
But neither method described there is detailed enough for me to write an algorithm on.
I would prefer the purely algebraic method described in the second post, but what ever works.
Here is an answer in Python I just ported from the Wikipedia article. There is no need for an algorithm; there is a closed form solution.
import numpy
from numpy import sqrt, dot, cross
from numpy.linalg import norm
# Find the intersection of three spheres
# P1,P2,P3 are the centers, r1,r2,r3 are the radii
# Implementaton based on Wikipedia Trilateration article.
def trilaterate(P1,P2,P3,r1,r2,r3):
temp1 = P2-P1
e_x = temp1/norm(temp1)
temp2 = P3-P1
i = dot(e_x,temp2)
temp3 = temp2 - i*e_x
e_y = temp3/norm(temp3)
e_z = cross(e_x,e_y)
d = norm(P2-P1)
j = dot(e_y,temp2)
x = (r1*r1 - r2*r2 + d*d) / (2*d)
y = (r1*r1 - r3*r3 -2*i*x + i*i + j*j) / (2*j)
temp4 = r1*r1 - x*x - y*y
if temp4<0:
raise Exception("The three spheres do not intersect!");
z = sqrt(temp4)
p_12_a = P1 + x*e_x + y*e_y + z*e_z
p_12_b = P1 + x*e_x + y*e_y - z*e_z
return p_12_a,p_12_b
Probably easier than constructing 3D circles, because working mainly on lines and planes:
For each pair of spheres, get the equation of the plane containing their intersection circle, by subtracting the spheres equations (each of the form X^2+Y^2+Z^2+aX+bY+c*Z+d=0). Then you will have three planes P12 P23 P31.
These planes have a common line L, perpendicular to the plane Q by the three centers of the spheres. The two points you are looking for are on this line. The middle of the points is the intersection H between L and Q.
To implement this:
compute the equations of P12 P23 P32 (difference of sphere equations)
compute the equation of Q (solve a linear system, or compute a cross product)
compute the coordinates of point H intersection of these four planes. (solve a linear system)
get the normal vector U to Q from its equation (normalize a vector)
compute the distance t between H and a solution X: t^2=R1^2-HC1^2, (C1,R1) are center and radius of the first sphere.
solutions are H+tU and H-tU
A Cabri 3D construction showing the various planes and line L
UPDATE
An implementation of this answer in python complete with an example of usage can be found at this github repo.
It turns out the analytic solution is actually quite nice using this method and can tell you when a solution exists and when it doesn't (it is also possible to have exactly one solution.) There is no reason to use Newton's method.
IMHO, this is far easier to understand and simpler than trilateration given below. However, both techniques give correct answers in my testing.
ORIGINAL ANSWER
Consider the intersection of two spheres. To visualize it, consider the 3D line segment N connecting the two centers of the spheres. Consider this cross section
(source: googlepages.com)
where the red-line is the cross section of the plane with normal N. By symmetry, you can rotate this cross-section from any angle, and the red line segments length can not change. This means that the resulting curve of the intersection of two spheres is a circle, and must lie in a plane with normal N.
That being said, lets get onto finding the intersection. First, we want to describe the resulting circle of the intersection of two spheres. You can not do this with 1 equation, a circle in 3D is essentially a curve in 3D and you cannot describe curves in 3D by 1 eq.
Consider the picture
(source: googlepages.com)
let P be the point of intersection of the blue and red line. Let h be the length of the line segment along the red line from point P upwards. Let the distance between the two centers be denoted by d. Let x be the distance from the small circle center to P. Then we must have
x^2 +h^2 = r1^2
(d-x)^2 +h^2 = r2^2
==> h = sqrt(r1^2 - 1/d^2*(r1^2-r2^2+d^2)^2)
i.e. you can solve for h, which is the radius of the circle of intersection. You can find the center point C of the circle from x, along the line N that joins the 2 circle centers.
Then you can fully describe the circle as (X,C,U,V are all vector)
X = C + (h * cos t) U + (h * sin t) V for t in [0,2*PI)
where U and V are perpendicular vectors that lie in a plane with normal N.
The last part is the easiest. It remains only to find the intersection of this circle with the final sphere. This is simply a plug and chug of the equations (plug in for x,y,z in the last equation the parametric forms of x,y,z for the circle in terms of t and solve for t.)
edit ---
The equation that you will get is actually quite ugly, you will have a whole bunch of sine's and cosine's equal to something. To solve this you can do it 2 ways:
write the cosine's and sine's in terms of exponentials using the equality
e^(it) = cos t + i sin t
then group all the e^(it) terms and you should get a quadratic equations of e^(it)'s
that you can solve for using the quadratic formula, then solve for t. This will give you the exact solution. This method will actually tell you exactly if a solution exists, two exist or one exist depending on how many of the points from the quadratic method are real.
use newton's method to solve for t, this method is not exact but its computationally much easier to understand, and it will work very well for this case.
Basically you need to do this in 3 steps. Let's say you've got three spheres, S1, S2, and S3.
C12 is the circle created by the intersection of S1 and S2.
C23 is the circle created by the intersection of S2 and S3.
P1, P2, are the intersection points of C12 and C13.
The only really hard part in here is the sphere intersection, and thankfully Mathworld has that solved pretty well. In fact, Mathworld also has the solution to the circle intersections.
From this information you should be able to create an algorithm.
after searching the web this is one of the first hits, so i am posting the most clean and easy solution i found after some hours of research here: Trilateration
This wiki site contains a full description of a fast and easy to understand vector approach, so one can code it with little effort.
Here is another interpretation of the picture which Eric posted above:
Let H be the plane spanned by the centers of the three spheres. Let C1,C2,C3 be the intersections of the spheres with H, then C1,C2,C3 are circles. Let Lij be the line connecting the two intersection points of Ci and Cj, then the three lines L12,L23,L13 intersect at one point P. Let M be the line orthogonal to H through P, then your two points of intersection lie on the line M; hence you just need to intersect M with either of the spheres.