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]
Related
I'm having trouble computing the intersection point of an ellipse and a line.
Let's say I have an ellipse at point (0/0) with radius 500. Additionaly I'm drawing a line from point (0/0) to (mouseX, mouseY).
First I check if the mouse coordinates are outside the ellipse by doing
if((mouseX*mouseX)+(mouseY*mouseY)) > 500*500){/*...*/}
Now, whenever the mouse coordinates are outside that ellipse, I want to draw the line not until the mouse, but until the 'border' of the ellipse. In order to do so, I must have the intersection point of the line and the ellipse.
Are there any libraries that simplify such trigonometric tasks? Is there any other more or less easy way to compute the intersection?
From what you've said, I'm making the following assumptions:
The ellipse is always circular (same radius all the way round).
The line is always being drawn from the centre of the circle.
If those are true then the problem is actually very simple. All you need to do is truncate the line so that its length is the same as the circle's radius, and that gives you the intersection point.
If the mouse is outside the ellipse:
Store the vector describing the line; in this case (mouseX, mouseY).
Normalize the vector (divide each component by the length of the line).
Multiply the vector by the radius of the circle.
The vector now contains the intersection point, relative to the centre of the circle.
You don't have to use a vector class for this, although it might help.
If your circle and line aren't starting on the origin (0,0) then you'll need to compensate. At step 1, subtract your new origin from (mouseX,mouseY). After step 3, add the origin back in to get the display coordinates.
I need to separate lines from each other and make an array of coordinates for each graphic. The problem is (see the red circled section on the pic) some graphics overlap, and i don't know how to write a program that will find this overlaps and separate them. (Now we think that lines only touch each other not cross.)
#Rethunk i maked thinning and got this result.
after thinning
If you know that the lines are initially separated, you could keep track of them going from left to right. For each line the top and bottom y-coordinates gradually change, while the x-coordinate is increasing. For each pixel you go to the right, you could start at the average y-coordinate and move up and down to find the new top and bottom y-coordinates for each line.
When two lines touch, their top and bottom y-coordinates will be the same. This can be detected by comparing the coordinates for lines that are next to each other. So lets say for example, line 4 and 5 overlap at a certain point. For these lines, you know which one is the higher line (4) and the lower line (5). Lets say yTopOverlap = 130 and yBottomOverlap = 160. We could divide the pixels between the two lines. In this case, make yTop 130 and yBottom 145 for line 4 and make yTop 146 and yBottom 160 for line 5. When the lines separate again, it is no longer necessary to modify their y-coordinates.
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.
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?