Geometry code gets tiresome after a while, but I want to finish this library, so here goes.
Basically, what's the most efficient way to move one line segment, A, so that it no longer intersects with another, B?
Both line segments are defined with a start point (x, y) and a vector describing how the segment extends from that point (eX, eY). An example of how the line segment is described is below:
The solution I'm looking for is where the line segment is moved (its extent is not modified in any way) to the nearest location where it does not intersect. An example:
What is the most efficient way to get this result?
EDIT: People have asked what I mean by "move" - I mean change the (x, y) coordinate of the line segment start point. This will translate the entire segment.
And the line segments exist on the Cartesian plane, and any x/y movement is allowed.
How about this: find the four vectors: two from red line's endpoints going perpendicularly to the black line, and two going perpendicularly from the red line to the black line's endpoints. Take the shortest of these vectors and move the red line along it.
Since you don't specify in which dimension you are free to move, I will assume that any are fine.
I assume your red line is characterized by a starting point (x,y), and a vector from there to the endpoint (eX,eY). Any point on the line is thus [0,1]*(eX,eY)+(x,y).
Lets find the point where lines cross. That's where a*(eX1,eY1)+(x1,y1) = (eX2,eY2)+(x2,y2) with a in [0,1].
If this crossing exists, you can just move the line so that it ends at this crosspoint, with a being the length you have to move.
(x1',y1') = (x1,y1) - a*(eX1,eY1)
This way, you move the starting point away until the crossing point you found before is the touching point of the two lines.
Related
In my application I work with OpenGL and large arrays of data.
One of the things I need to do is I receive multiple "simple" polygons that can be either convex or concave. By simply I mean geometrical definition - polygons without holes and intersecting edges.
I receive this poligons in the form of linked loop of points (vertices), where each Vi is connected to Vi-1 and Vi+1 points and nothing else. The last point is also connected to the first, giving us a loop as a result.
I will say right now that I figured out polygon triangulation, and it works like a charm (I used ear-clipping method for now).
But, I need to build LOTS of those polygons, millions of them. To minimize the load I decided to split my world into "chunks", squares like in Minecraft. I then use frustum culling and other optimizations methods to either render or discard/simplify these chunks and meshes in them.
So, what I want to do is: if the polygon I received lies in several different chunks - then I want to split it into meshes for each of those chunks. Basically, I want to create multiple polygons that are divided on chunk borders, and THEN triangulate them and create meshes for rendering.
As a result I want to have one (or several) contours that I can then triangulate.
here's a picture of my task:
I made an algorythm for splitting it myself that I thought would work, but I found that it only works if the contour in any given chunk doesn't break up, like you see in the example (in other words if there's only one figure in any given chunk).
I thought to ask here if anyone knows any good algorythms for that task? I'll probably come up with something myself, but practice shows that there's almost always a better and simpler ready-made solution out there. I'd really appreciated it if someone could give me a useful link or an article, if not with a solution itself, then something that could give ideas.
I'm not at work at the moment, because it's weekend, so I'll be trying/testing the things on Monday.
What I came up with at the moment, however, is a very simple solution.
test all points in the contour.
If point i and point i+1 don't belong to the same chunk (which I can test easily):
then find an intersection between chunk border and the line made by these two points.
add this new point to the contour between the original two points.
When every edge of the polygon was tested like that - triangulate it.
because we have points on the edges of the chunks - then during triangulation the triangles will fit exactly into chunk borders
for each triangle, decide which chunk it belongs to and generate the mesh in THAT chunk.
I won't go into details of some optimization ideas - like not needing to evaluate resulting triangles if the entire figure fits within the same chunk.
This is a stab in the dark at writing some pseudo code that may work. Feel free to implement it and select your own answer with the actual code if it works.
First, convert your vertex array to a double linked list that loops from the last element to the first element. Or it may be better to model it as an undirected graph instead, because a point may have missing neighbours.
Then apply the algorithm below for each quadrant, starting with the full polygon each time. You can cut down the size of that polygon by culling vertices that are outside of the quadrant and at least 1-neighbour away from edges that cross the cutting lines.
// We need to keep track of the points that we insert on the cutting lines
let LIX = List of X-Cut-Line Intersection Points
let LIY = List of Y-Cut-Line Intersection Points
foreach Edge AB in Poly where A is inside the quadrant, B outside
// Insert points into the polygon that intersect the X-Cut-Line
if AB Intersects X-Cut-Line
let Ix = Intersection Point
Insert Ix between AB so that A<->B becomes A<->Ix
B can be removed from the polygon now
Add Ix to LIX
// Insert points into the polygon that intersect the Y-Cut-Line
if AB.Intersects Y-Cut-Line
let Iy = Intersection Point
Insert Iy between AB so that A<->B becomes A<->Iy
B can be removed from the polygon now
Add Iy to LIY
// Then iterate pairs of points along each cutting line to join them
sort LIX by X Ascending
while (LIX.Length > 0)
let P0 = LIX[0]
let P1 = LIX[1]
Link P0 and P1
Remove P0 and P1 from LIX
sort LIY by Y Ascending
while (LIY.Length > 0)
let P0 = LIY[0]
let P1 = LIY[1]
Link P0 and P1
Remove P0 and P1 from LIY
Finally, you can look for cycles in the resulting linked list/graph to find the new polygons. Each cycle should in theory not contain points from the other cycles already detected so it is an individual polygon.
I want to draw a dashed line, but can't find the best way for this issue.
There doesn't seem to be an in-built function, so you will have to do that "manually":
Choose the coordinates for the start of the line.
Choose the coordinates for the end of the line.
Using those two vectors, calculate the slope between those two points.
Recursively (in a loop) use the slope to calculate the next coordinate along the path to place a dot / dash.
Draw a very short (e. g. 1px) line segment representing the dot / dash.
[source]
I have an image such as this:
and I need to calculate the orientation of it. In this case the shape is pointing towards the top left of the screen. Accuracy isn't hugely important as long as 3 or 4 calculations average out to within 5 degrees or so of the actual orientation (it will be moving slightly).
Can anyone point me towards an algorithm to do this? I don't mind if the orientation is returned as a double or as a vector.
If the image is always T-shaped, you can simply get the furthest pair of pixels, then find the furthest pair from either both of those (the edges of the T), find which is further from the other two, draw a line from that one to the middle point of those two.
You can further refine it by then finding the base of the T by comparing the middle line with the edges of the base, and adjusting the angle and offset until it is actually in the middle.
The definitive solution is impossible I guess, since requires image recognition. I would project the 2D image onto axis, i.e. obtain the width and height of the image and get direction vector from these values taking them as components.
First, a couple of assumptions:
The center and centroid are "close"
The descending bar of the T is longer than the cross-bar
First, determine the bounding rectangle of the image and find the points of the image that lie along this rectangle. For points that lie along the line and are a certain distance from one another (say 5 pixels to pick a value) you'll need to only take 1 point from that cluster. At the end of this you should have 3 points, i.e. a triangle. The shortest side of the triangle should be the cross-bar (from assumption 2), i.e. find the two points closest to each other. The line that is perpendicular to the line crossing those two points is then your orientation line, i.e. find the angle between it and the horizontal axis.
I would try morphological skeletonization to simplify the image, followed by some straightforward algorithm to determine the orientation of the longer leg of the skeleton.
The solution in the end was to use a Convex Hull Algorithm, which finds the minimum number of points needed to enclose a shape with a bound.
To get right to it: Say I've got two specific pixels a & b, as well as a list of random others. I now want to check all pixels in that list if they are on the line which passes through a & b (but which is NOT restricted to have a & b as endpoints!)
I looked at Bresenham's line algorithm, but that seems to only find the points between a & b?
I've also had a look at linear equations in general but I'm kind of stuck on how to properly discretize the continuous line into pixels...
(I'm trying to implement a random sampling algorithm in java, which tries to find the line with the most pixels on it, if that matters).
Thanks a lot for any help with this :)
I strongly suggest that you compute the distance between the point and the line.
If you use one pixel as unit of measurement (which you will if you work in a pixel coordinate system), you can simply check if the distance from the point to the line is < 1.
Here's how to compute the distance between a point and a line:
Minimum Distance between a Point and a Line
Wolfram: Point-Line Distance--2-Dimensional
You could search not between a and b, but between the points that are on the same line, but out of the borders of image. You can get them simply putting vector (b-a) necessary amount of times to the a side and to the b side. And the use your Bresenham's line algorithm.
As for more concrete answers, define the question better, please. Do you consider pixels as points or squares or circles? What is "pixel is on the line" in your terms? What width has your line? You are looking for the line with max number of pixels on the cm length or in the rectangle borders? Among what lines do you search?
I am making my own class diagram app in Swing/AWT but i stopped at this functionality:
I want to draw a line between the Class rectangle that already selected and to the target Class rectangle, but line has a feature which is whenever i move one of the rectangles the line that join them get bend in a straight fashion following the moving rectangle , i hope the following picture demonstrate what i want to achieve:
A general guideline or a sample code is highly appreciated
I don't know Java, but the steps you could follow are these:
Find the middle of each line of the rectangles (should be easy, just avarage x1+x2 and y1+y2)
Determine the edges that are closest to each other using Pythagoras formula on the points you got in the previous step.
Start drawing a line starting at xa,ya (first point you got in the step above), and draw it in the direction away from the rectangle. You should know this direction, because you can know the line segment this point is on.
Do the same for xb,yb (point on the second rectangle). If the lines are in opposite directions, you should draw them to halfway xa-xb or ya-yb (depending on if you're drawing horizontally or vertically). If they are perpendicular (is that the right word?) you should draw them to the point where they cross, so you draw the line from xa,ya to xa,yb or xa,ya to xb, ya, depending if you draw the horizontal or vertical line.
There should be some additional check to see if the rectangles overlap. You should not draw lines in the same direction for example. Maybe it would suffice for you to draw just a diagonal line between the two points in those cases where you cannot determine how to draw these straight lines.
For the implementation you could build a line class that uses the observer pattern to listen to the two rectangles it follows, so it can update itself whenever one of them moves or resizes.
http://java-sl.com/connector.html
Hope this helps.
Try with observer pattern. All lines that are connected with a moving object should be notified with new position of the object and 'bent' properly. Of course, first implement some logic that will connect 2 objects.
try creating a class named "ConnectingLine" or similar. this class will then have several segments (that's the name of these line parts in dia, which is currently my favorite uml modeling tool) which will be calculated one by one. you'll have a sepaparate class for this of course ;) called maybe "LineSegment". i think this should make it easier for you to perform the mathematical calculations required to perform this task.
this could also enable making segments "auto routed or not" easy d(^_^)b