Pretty easy. (Easier yet, IF the lines were drawn at right angles. If they were, then we could do it in literally one line of code.) But I can see they are NOT exactly perpendicular to each other, so this will take a couple more lines of code, but not many.
Anyway, just create a simple triangulation.
triXY = [-1.5 -1.5;1.5 -1.5;-1.5 2;1.5 2;0 0.25]
triXY =
-1.5000 -1.5000
1.5000 -1.5000
-1.5000 2.0000
1.5000 2.0000
0 0.2500
triangles = [1 2 5;1 3 5;2 4 5;3 4 5];
T = triangulation(triangles,triXY)
T =
triangulation with properties:
Points: [5×2 double]
ConnectivityList: [4×3 double]
trimesh(triangles,triXY(:,1),triXY(:,2))
plot(xy(:,1),xy(:,2),'ro')
Next, we consider what tools a triangulation can use.
methods(T)
Methods for class triangulation:
barycentricToCartesian edgeAttachments featureEdges isConnected pointLocation vertexAttachments
cartesianToBarycentric edges freeBoundary nearestNeighbor size vertexNormal
circumcenter faceNormal incenter neighbors triangulation
In there, we see that pointLocation is a method for this class. That may help me.
help pointLocation
--- help for triangulation/pointLocation ---
pointLocation Triangle or tetrahedron containing specified point
TI = pointLocation(T, QP) returns the index of the triangle/tetrahedron
enclosing the query point QP. The matrix QP contains the coordinates
of the query points. QP is a mpts-by-ndim matrix where mpts is the
number of query points and 2 <= ndim <= 3.
TI is a column vector of triangle or tetrahedron IDs corresponding to the
row numbers of the triangulation connectivity matrix T.ConnectivityList.
The triangle/tetrahedron enclosing the point QP(k,:) is TI(k).
Returns NaN for points not located in a triangle or tetrahedron of T.
TI = pointLocation(T, QX,QY) and TI = pointLocation (T, QX,QY,QZ)
allow the query points to be specified in alternative column vector
format when working in 2D and 3D.
[TI, BC] = pointLocation(T,...) returns, in addition, the Barycentric
coordinates BC. BC is a mpts-by-ndim matrix, each row BC(i,:) represents
the Barycentric coordinates of QP(i,:) with respect to the enclosing TI(i).
Example 1:
% Point Location in 2D
T = triangulation([1 2 4; 1 4 3; 2 4 5],[0 0; 2 0; 0 1; 1 1; 2 1]);
% Find the triangle that contains the following query point
QP = [1, 0.5];
triplot(T,'-b*'), hold on
plot(QP(:,1),QP(:,2),'ro')
% The query point QP is located in a triangle with index TI = 1
TI = pointLocation(T, QP)
Example 2:
% Point Location in 3D plus barycentric coordinate evaluation
% for a Delaunay triangulation
x = rand(10,1); y = rand(10,1); z = rand(10,1);
DT = delaunayTriangulation(x,y,z);
% Find the tetrahedra that contain the following query points
QP = [0.25 0.25 0.25; 0.5 0.5 0.5]
[TI, BC] = pointLocation(DT, QP)
See also triangulation, triangulation.nearestNeighbor, delaunayTriangulation.
Documentation for triangulation/pointLocation
doc triangulation/pointLocation
Other uses of pointLocation
DelaunayTri/pointLocation
The index returned indicates where the corresponding point fell. You can skip the plots, as you wish.
A second solution is also not difficult. This relies on an affine transformation, if the points in the plane. Think of the data as a coordinate system around an origin at the point (0,0.25). Thus, where the lines cross. A problem is, the lines have the wrong slopes, as they are not perpendicular to each other. We can resolve that simply enough, by scaling y by a factor of 3/3.5. And then we can rotate the coordinate system by 45 degrees.
The final transformation, if we have an array xy of size nx2:
T = @(xy) (xy - [0 0.25]).*[1 3/3.5]*[cosd(ang) sind(ang);-sind(ang) cosd(ang)];
THe function handle T uses capabilities introduced in R2016b. Earlier releases would use bsxfun.
If you look at the transformation, first notice I subtract off 0.25 from y only, so x is unchanged. Effecively, now the lines cross at the origin.
Next, I scaled the problem, so multiplying y by a factor of 3/3.5. That effectively corrects the two lines so they are now perpendicular after the rotation. (This was the error @Walter Roberson made in his suggestion.) Finally, the 2x2 matrix I multiply by there is a rotation matrix. I've used sind and cosd there so it is clear how it was formed. See what the transformation did to the points listed in triXY.
triXY
triXY =
-1.5000 -1.5000
1.5000 -1.5000
-1.5000 2.0000
1.5000 2.0000
0 0.2500
T(triXY)
ans =
-2.1213 0
0 -2.1213
0 2.1213
2.1213 0
0 0
The 5th point now lies at the origin. And each of the other points have been properly scaled, and then rotated. Now, how can we use the above transformation? I'll rotate the points in the array xy, into a new array, then plot the transformation.
plot(xy(:,1),xy(:,2),'o')
trimesh(triangles,triXY(:,1),triXY(:,2))
plot(newxy(:,1),newxy(:,2),'x')
plot([xy(:,1),newxy(:,1)]',[xy(:,2),newxy(:,2)]','r-')
As you can see, the entire system of points has been shifted, stretched just a bit, and then rotated.
How can we now use this? If the final location of a point was originally in the top triangle, then it is now in the first quadrant. In that case, both the new x and the new y are positive numbers. So now we can decide which of the original (quasi-)quadrants a point lies in, merely by testing the signs of the points after rotation.
T(xy) >= 0
ans =
0 0
0 0
1 1
1 1
0 0
0 0
1 0
0 1
0 0
1 0
So if both results are 1 there, then the point lies now in the first quadrant. If both results are zero, then the point lies in the 3rd quadrant. And then you can easily distinguish the other quadrants too. A nice trick is to use a binary to decimal encoding.
As you can see, that yields an integer, so 0,1,2,3, depending on which of the original regions a point fell in. I'll let you decode which of the regions was which.