Point inside a rectangle - On which side of the diagonal is it? - coordinates

So, I have a rectangle divided into two triangles and I have a point:
I know the coordinates of the four vertices of the rectangle (A, B, C and D) and I know the coordinates of P. What I need to know is in which of the two triangles is the point.
I think it's a really simple thing to do, but apparently I can't figure it out by myself.
Any help?

There are couple of ways to tackle this. But the easiest and most straight forward is to check for the slope.
For your example, if slope AP is lesser than slope AD then point P is above the AD line and vice versa. If the slope is the same, then it is on the same line.
Edit:
Assume the following
A -> (0,3)
B -> (3,3)
C -> (0,0)
D -> (3,0)
Then the slope AD is (3-0)/(3-0) = 1. Assume that P is at (2,2), then slope AP is (2-3)/(2-0) = -1/2.
We have AD > AP, thus point P must be above the line AD. Notice that point P needs to cross AD in order to be below the line, and thus the slope changes.

You could consider vector AP and vector AD. Compute the cross product of these two vectors. The sign of the cross product will tell you what side of the diagonal the point is on.
More information: http://en.wikipedia.org/wiki/Cross_product

Related

Catmull Clarke doesn't preserve planar normals

In the toy example I show, one of the surface normals is clearly incorrectly pointing inwards. I can create a new cube with the normals outward facing as expected, but after processing with Catmull Clarke, there is no guarantee that all normals will remain extant facing.
Since I'm using quadrilaterals by necessity, I know I can fix the face by transposing the vertice order i.e. [a b c d] -> [d c b a] and thus fix the normal. But how do I determine that a given face's normal is pointing the wrong direction?
(not enough rep to embed) https://gyazo.com/e20576e700196a43a2378eb055a71b38
You can check for sign of the dot product between the face normal vector and the vector from the cube's centroid to any point on the face.
Let's say the normal vector of the face [a,b,c,d] is n. Next, compute the centroid of the cube by averaging its 8 vertex coordinates, and let's call it p.
Then, calculate dir = dot(n,(a-p)). If dir > 0, the normal n is pointing outwards from the cube. If dir is negative, you have to flip the normal.
This method will work for the faces of any convex polyhedron. If you're dealing with non-convex polyhedra, you will have to use an approach like the one mentioned here.

Given 4 line equation of a convex quadrilateral, how can I find coordinate of 4 corner?

I have four line equations y=mx+b and there is 6 intersections point.
It is known that those line form a convex quadrilateral.
How can I find which four intersection form the quadrilateral (preferably in order)?
First Concave QUAD:
The 6th intersection point should be also in 1) I just forget to draw it there.
compute all the intersection points and pair them to lines
l1: p2,p5,p6
l2: p1,p4,p6
l3: p1,p2,p3
l4: p3,p4,p5
l means line and p means intersection point
determine if point is only edge or also middle
so middle point is if you got another point from each side of line it belongs. In other words if you convert point to its parameter position (or distance from some start point) then middle point is in between the other two points distances. The parameter/distance you directly obtain while intersection computation but in case you don't you can use this:
t(p) = dot(p-A,B-A)
where A,B are line endpoints, p is queried intersection point and t(p) is its scalar "distance" from A.
So find out which points are only edge e and which are middle m:
l1: e2,e5,m6
l2: e1,e4,m6
l3: e1,m2,e3
l4: e3,m4,e5
now if any point is at least once middle then it is partially middle, if it is only edge then it is edge and if it is only middle then it is middle:
edge: p1,p3,p5
partial: p2,p4
middle: p6
construct polygon
So the edge points we have to use. The partial points we skip (as they lie on already used line) and finally we also use middle point. We know our polygon will be:
(p1,p3,p5) + (p6)
Now we need to find where the concave middle point p6 will go. There are 3 combinations:
e1,m6,e3,e5
e1,e3,m6,e5
e1,e3,e5,m6
we know that m6 belong to l1,l2 and l1,l2 has also p2,p5,p1,p4 from which are edge points only: e1,e5 so the m6 will be placed between them so the correct solution is:
e1,e3,e5,m6
Now Convex QUAD:
if we take advantage from #1,#2 then to form convex quad we have to use the middle and partial middle points and chose one of the pure edge points. Select the one that is not belong to line with pure middle point. So we have to use:
(p2,p4) + (m6) + one_from(e1,e3,e5)
the m6 is not belonging to l3,l4 so we need find edge belonging to both which is e3 so
(p2,p4) + (m6) + (e3)
Now we just have to find out the order. middle point and edge point will not be near themselves so you got 2 solutions:
p2,m6,p4,e3
p2,e3,p4,m6
Both are correct they are just reverse of themselves (differend polygon winding rule) so you can chose the one you need based on z coordinate of cross product of any two neighboring vertices.
Solve systems of linear equations for every line pair
y = m[i] * x + b[i]
y = m[j] * x + b[j]
where m[i], b[i] are coefficients of i-th line and get (x,y) for intersection.
If there are exactly 6 intersection (no parallel lines, no degeneracies), then every line has three intersection points in row. Two intersections are "outer" and one (middle) is "inner", so there are three inner and three outer points.
Seems that convex quadrilateral always contains all the three inner points, so we can separate inner points, and get corresponding fourth point.
For example, if lines are designated as A,B,C,D, and intersections AB, AC, CD are inner, then fourth point is intersection BD (B and D are unique in intersection list)
P.S. Note that line equation form y=mx+b is not suitable for all possible lines (not for vertical lines), so it would better to use more general form like A*x + B*y + C = 0 or other.

Sorting array of points in clockwise order

Is there such an algorithm to sort an array of 3D points in clockwise order?
I'm specifically dealing with right triangle in my case so only 3 points. (for build mesh)
Assume you have two edges connecting your three vertices.
E1 = V2 - V1
E2 = V1 - V3
They span a triangle. You can calculate the triangle's normal N like this:
N = cross(E1, E2)
This tells you which direction the triangle is facing. You can calculate whether the triangle is facing towards, or away from, a certain point of view P by projecting N onto the distance of your triangle from P.
D = V1 - P
d = dot(N, D)
If d is positive, the triangle looks away from P, if it is negative, it faces P.
You are now able to judge for every set of (V1,V2,V3) whether they are sorted correctly or not. If not, just swap V2 and V3 and they will be.
There is one pitfall though. If you are trying to build the hull of a closed mesh, the requirement is that all triangles are facing towards the outside. This can not be modelled by trying to make all triangles face a certain point, because that point would have to be different for each triangle. If the mesh is convex through, you can model it by requiring that all triangles face away from a certain point, which lies inside the convex mesh.
The algorithm for sorting is not difficult. The problem is, what plane that those points lie on. And which side it facing
Only a bunch of points cannot clockwise or counter clockwise by itself. You need a plane and a side to reference those point
edit : Actually what I used to said is a bit inaccurate. What you really need is a position and direction to reference, not a plane

How to find if a point is inside of a polygon using Racket

I am working an a project that, given a specific latitude and longitude coordinate, outputs the neighborhood that the point resides in. I have the latitude and longitude coordinates that make up the boundaries of several neighborhoods within a city. I have to read the neighborhood data from a file, and also read the test-points from a file. I am using the Racket programming language.
So far I have been able to read the files and create a list of points for each neighborhood, and now I am stuck. I wanted to create a polygon for each neighborhood, and then have a method that checks to see if a point lies inside that polygon. However, I cannot figure out how to do that using Racket.
Can anyone help me find out how to solve if a point is inside that polygon, or perhaps a better way to go about solving the problem?
I won't post any code for now because I don't want to solve the homework/assignment. However, I'll post some hints.
Look at the following picture:
How can we know that C is between the edges OA and OB and D is outside? It simple: we compare some angles: if the angle between OC and OA is smaller than the angle between OB and OA then C is clearly closer to OA than OB is.
Now, how do we get the angle knowing only some vectors? We can use the cosine which is monotonous: it decreases with the increasing argument. Thus, the cosine of the angle between OC and OA is greater than the cosine of the angle between OB and OA which is in turn greater that the cosine of the angle between OD and OA.
Next step is to figure out how to compute the cosine. The vector dot product helps: it's value is cosine of angle times greater than the product of operand's lengths. That is:
cos(OC; OA) = dotproduct(OC; OA) / (length(OA) * length(OC))
The dotproduct in 2D is simple:
dotproduct(OC; OA) = (C.x - O.x) * (A.x - O.x) + (C.x - O.x) * (A.x - O.x)
Combining all of the above you should have a simple test to check whether your point is in the same situation as C or as D: closer to one edge than the previous edge or not.
Now, you'll have to repeat this for every edge of the polygon and you're done. You can do this with a fold if the test is a predicate.
Note: this only works if the polygon is convex. For a concave polygon you'll need to add more tests.
Second note: In the figure, what will happen if D or C or both are below the OA line? Think about this and check if it means some more changes to the above fold method.
Last note: In a few weeks I'll post a complete code for that, assuming the assignment is over. Also, at that time I'll answer the question in the above note.
You need to start by collecting the segments of your polygon.
For the point P, you need determine if the horizontal ray starting from the point P intersects the side (segment).
If you count how many segments the point's horizontal ray intersects, then an odd number will be inside, and an even number will be outside.
(define (point-in-polygon? point polygon)
(odd?
(for/fold ([c 0]) ([seg polygon])
(+ c (if (ray-cross-seg? point seg) 1 0))))))
I have a complete solution in https://github.com/StevenACoffman/lat-long-kata-racket
An alternative Ray Casting Algorithm in Racket is https://rosettacode.org/wiki/Ray-casting_algorithm#Racket as well as in 35 other programming languages.
A more detailed walkthrough is available: https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/

matlab: area under overlapping circles

I have a question to you...
Imagine square with size A x A. Now lets simulate circles with diameter of d, randomly distributed within this square, something like on the image below (in this case d's are the same, but its not the rule, they might be also randomly distributed within some range like d1 to d2).
Lets say that circles are described in matrix as:
circles(1, :) = [x, y, d];
circles(2, :) = [x, y, d];
...and so on
where x, y are coordinates, d is diameter. Now the question is, how to simulate this circles, until given crowding parameter c is reached? c is simply defined as: c = yellow area / square area (in this case A^2).
And the second thing - lets say that everything is simulated and I want to check if some coordinate (x,y) is within or outside yellow area... How to do it? I was doing it by checking if my (x,y) is within area of each circle (but its getting more difficult when instead of circles I use i.e. round shape rectangles), one by one, but there must be some better way of doing it.
Thanks for help : )
Here is an approach that should do the trick:
Start with a large empty matrix (big enough to guarantee that every shape generated is fully inside the matrix). Suppose we do it like this color = zeros(100)
while we have not yet reached the cowding ratio: the midpoint and diameter of one circle, I assume you can manage this
change the color of all points in the circle, for example by setting it to one.
Calculate the crowding ratio (something like c = mean(mean(color))
Note, if you only want to use part of the matrix (enable shapes to fall partially out of the picture) this can for example be achieved by using mean(mean(color(11:end-11)) in step 4, ignoring the 10 pixels near the edge.
Now if you want to know whether point (x,y) is yellow, simply check the value of color(x,y). Or if you want to ignore the edges check color(x+10,y+10)