Given three points (x1,y1),(x2,y2),(x3,y3) on a Cartesian plane can u find a fourth point such that these four points form a rectangle using c program
Tha code I am expecting
Related
Building on the discussions here and here. I'm trying to compute the shortest distance between a 3D line and a 3D triangle.
I'm using barycentric coordinates to determine whether or not the point is inside the triangle. So given a triangle defined by vertices UVW and a line defined by point AB, I first compute the intersection of line AB with the plane defined by UVW. Let's call this intersection P and assume I've already done the checks to verify whether or not the point actually intersects the plane at all.
I then compute barycentric coordinates (S,T) such that S is defined along the edge UV and T is defined along the edge UW. Naturally, if 0≤S and 0≤T and S+T≤1 then P is on the triangle (or its edge) and my distance to the triangle is obviously zero.
If that's not true then P is outside the triangle and I need to compute the distance. The guidance of from the first link says to project point P onto all three edges to get three candidate points. Adding those points to the three triangle's vertices, you then have six points to test against.
Isn't it easier than that, though? If T<0, then don't you already know that UV is the closest edge and you only have to test against the projection of P onto that line? Similarly, if S<0 then UW would be the closest edge. If T>0 and S>0 then VW is the closest edge.
Thus based on the signs of S and T you already know the closest edge and only have to compute the distance from P to its projection onto that edge. If the projection isn't inside the triangle, then the closest point is either vertex. Thus your computations are about 1/3 of the proposed methods.
Am I missing something here, or is this a valid optimization? I'm fairly new to barycentric coordinates and their attributes.
It turns out that the problem of closest distance from a point and from a line are very similar and can both be reduced to a pure 2D problem.
Distance from a point
By Pythagoras, the squared distance from a point to a point of the triangle is the sum of the squared distance to the plane of support of the triangle and the squared distance of the projection of the point to that plane.
The latter distance is precisely the distance from the normal line to the triangle.
Distance from a line
Looking in the direction of the line, you see the projected triangle and the line is reduced to a single point. The requested 3D distance is equal to the 2D distance seen on the projection.
To obtain the desired coordinates, you use an auxiliary coordinate frame such that Z is in the direction of the line (and XY is a perpendicular plane); for convenience, choose the origin of the new frame to be on the line. Then by just ignoring Z, you get a planar problem in XY. The change of coordinates is an affine tranformation.
Point vs. triangle
Consider the three triangles formed by the origin (projection of the point/line) and a pair of triangle vertices (taken in cyclic order). The signed area of these triangles is a mere 2x2 determinant.
If the three areas have the same sign, the point is inside. Otherwise, the signs tell you where you are among the six surrounding regions, either past an edge or past a vertex.
On the upper figure, the point is inside (three positive areas). On the other figure, it is outside of the top-right edge (one negative area). Also note that dividing an area by the length of the corresponding side, you get the distance to the side. (Factor 2 omitted.)
The total work is
compute the affine frame;
convert the coordinates of the 3 or 4 points;
compute the three signed areas;
if inside, you are done;
otherwise, if in an edge region, compute a distance to a line and two distances to points;
otherwise you are in a vertex region, compute two distances to lines and one distance to vertex.
I have to create a function in MATLAB that performs the following task:
Input:
p polygon in the form
p = [x1,y1; x2,y2; x3,y3; x4,y4...]
s struct with the segment from A to B
s = struct('A',[x,y],'B'[u,w])
Return:
1) An integer indicating how many intersections there are between the segment and the polygon (e.g., 0,1,2)
2) A new segment from A to B, where A is the first intersection or the initial point of the input segment and B the second point of the intersection or the last point of the segment input.
I have an idea on how to do it by using the function inpolygon. I have been reading how to use this function, and know that to use that, I should provide a query point and the coordinates of the polygon vertices. It will return 1 or 0 depending on whether it is inside or not.
My question is, how can I get the query point of the segment that is placed exactly in the boundary (in the case that the segment intersects with it)?
If you have the Mapping Toolbox installed, you could use polyxpoly. As this is a rather basic problem, there are quite a few free MATLAB-codes out there on the File Exchange. Here is what I found for the search term 'polygon intersect':
2D Polygon edges intersection by Bruno Luong
Find the intersection points of the edges of two 2D polygons, a simple function made to follow up a Newsgroup discussion
Curve Intersect 2 by Sebastian Hölz
This file is based on the Curve Intersect function by Duane Hanselman. It extends the scope of the function to handle arbitrary lines / polygons, which may also have vertical segments or segments with non-increasing x-values.
Curve intersections by NS
While a few other functions already exist in FEX that compute the
intersection points of curves, this short piece of code was written
with speed being the highest priority. No loops are used throughout,
taking full advantage of MATLAB's vectorization capabilities
Fast and Robust Curve Intersections by Douglas Schwarz
This function computes the (x,y) locations where two curves intersect. The curves can be broken with NaNs or have vertical segments. It is also very fast (at least on data that represents what I think is a typical application).
geom2d by David Legland
[...] derive new shapes: intersection between 2 lines, between a line and a circle, parallel and perpendicular lines
In MATLAB's function of Voronoi diagram, the vertices of edges at infinity are plotted at some distant point. Have a look at the first diagram on the page here. The first point from the top on Y-axis is (0,0.75). (Though it is extended beyond the bounds of the image). I know if I run the following matlab function:
[vx,vy]=voronoi(x,y)
I can get the coordinates of the vertices, but they will be beyond the bounds of the plot. Is there any way to get the coordinate in bounds of the plot (for example, (0,0.75) as mentioned above).
All you need is to detect which of the vx,vy crosses the axes (using find or logical conditions, find(vx<0) , find(vy>1) etc...) , and then apply the equation of the line y=a*x+b. For the point you wanted (which happens to be the 19th col of vx,vy, the slope a is:
a=diff(vy(:,19))/diff(vx(:,19));
and the intersection with y axis is given by b:
b=vy(1,19)-a*vx(1,19)
b =
0.7546
To calc b I picked the first point [vx(1,19),vy(1,19)] but this of course works also for the second point, i.e. b=vy(2,19)-a*vx(2,19)
This question already exists:
Closed 10 years ago.
Possible Duplicate:
intersection of line and circle with different slope
I have line which plotted by pp=randi([-400 400],2,2) then x=pp(:,1) and y=pp(:,2). I have a circle with centre (a,b) with radius r
I want to check the intersection point of circle and the line.
I have used polyfit command to check the slope and intercept. Then I used lincirc command but the problem is if the line crosses only one point then the other point is also shown.
For example, if the line crosses one side and stops in the middle, it shows the other point as well which will not cross the boundary
You have a circle radius r centred at (a,b). You have a line. If you have plotted these points, you must have your data stored in x and y vectors, so you take the first and last of elements each as (x,y) coordinates. The first pair form the line start point and last pair the end point. Refer to these points as (c1,d1) and (c2,d2). Assuming that your lincirc function tells you there are 2 intersection points between line and circle, calculate
A1 = (c1-a,d1-b)
A2 = (c2-a,d2-b)
Now if
norm(A1,2) < r
then endpoint (c1,d1) is inside your circle, if
norm(A2,2) < r
then endpoint (c2,d2) is inside your circle.
If one of the points is inside the circle, then you only have one intersection point.
If neither point is inside the circle, then you know that your line crosses the circle twice (assuming that your lincirc function tells you there are 2 points)
If both points are inside the circle, then your lincirc function is lying to you.
This question already exists:
Closed 10 years ago.
Possible Duplicate:
intersection of line and circle with different slope
I have line which plotted by pp=randi([-400 400],2,2) then x=pp(:,1) and y=pp(:,2). I have a circle with centre (a,b) with radius r
I want to check the intersection point of circle and the line.
I have used polyfit command to check the slope and intercept. Then I used lincirc command but the problem is if the line crosses only one point then the other point is also shown.
For example, if the line crosses one side and stops in the middle, it shows the other point as well which will not cross the boundary
You have a circle radius r centred at (a,b). You have a line. If you have plotted these points, you must have your data stored in x and y vectors, so you take the first and last of elements each as (x,y) coordinates. The first pair form the line start point and last pair the end point. Refer to these points as (c1,d1) and (c2,d2). Assuming that your lincirc function tells you there are 2 intersection points between line and circle, calculate
A1 = (c1-a,d1-b)
A2 = (c2-a,d2-b)
Now if
norm(A1,2) < r
then endpoint (c1,d1) is inside your circle, if
norm(A2,2) < r
then endpoint (c2,d2) is inside your circle.
If one of the points is inside the circle, then you only have one intersection point.
If neither point is inside the circle, then you know that your line crosses the circle twice (assuming that your lincirc function tells you there are 2 points)
If both points are inside the circle, then your lincirc function is lying to you.