Intersection between square shaped nodes and edges - java

I have a ŁŽquestion about the graphstream library
I want to write a program that counts the number of intersections between the nodes that are represented as a square with a specified size.
The main Problem is that, the nodes will be visualized as squares but if the coordinates of these nodes are just points x,y and here is exactly my Problem, because I want to check the intersections with the edges with the x,y coordinates but I can not do this properly!
because in some cases the corner of the square or its edges intersect with the edges but the original point that represents the square or the node in graphstream does not, I tried all the possible solutions with collinear points , slopes and everything but nothing worked for me, the question is, is there anyway in Grpahstream so that I can find out the coordinates of the edges or the corners of the square so that I can check with the line segment equation if they intersect ?
I mean something like that as shown in the picture
https://i.stack.imgur.com/PG4Dv.png
Best regards

Related

How to find overlapping areas?

the goal is to find all the rectangles you get when you come from all sides (north, west, east, south) in an orthogonal polygon. However, I can't get any further on concrete implementation.
So here is what I've done so far.
First: I have a list of coordinates (in a XML file), I want to read them into Java to determine the points and the edges of the polygon.
So now coming to my main problem. I want to find all rectangles that lie within this polygon,
[![the polygon]starting from all sides (north, east, south, west). I have a problem with this step. It occurred to me to use a SweepLine algorithm, but I am unsure how to implement it to get the desired result (overlapping rectangles coming from all sides). For that I have painted those pictures to clarify what I mean. If you come from the north edges of the polygon you would find the green rectangles. [![rectangles in the polygon coming from the north edges]
If you come from the west edges of the polygon you would find the red rectangles.
[rectangles in the polygon coming from the west edges]
and with that the overlapping rectangles..
[overlap of green and red rectangles]
However, I'm not sure how to do this the most easiest way. I researched a lot sweep line Algorithms, but I'm not sure how to implement it and whether this is an efficient way. My goal is to find those rectangles and save them in a proper way so that I can use them for further steps (e.g. finding positions in the polygon where many rectangles overlap)
Maybe someone could help me with that. Would appreciate this a lot!
If you already know all the edges and have determined whether they face North, South, East or West, determining the rectangles could be done like this:
Iterate through the edges.
Let's say we have a north-facing edge between (n.x1, n.y) and (n.x2, n.y). The corresponding rectangle must have its north-facing edge be the one we just looked at and its south-facing edge must, at least in part, consist of one or more south-facing edges.
We iterate through all south-facing edges and find the ones such that at least one point on the edge has an X coordinate within the range (n.x1, n.x2).
Additionally, they need to actually be south of the north-facing edge, so keep the ones where s.y > n.y.
Now, what we have left is a list of all south-facing edges that could possibly limit our rectangle's height. However, the rectangle is limited by whichever is the closest, so we simply pick the remaining south-facing edge, s, with the lowest y coordinate.
We now have a rectangle between (n.x1, n.y) and (n.x2, s.y).
Corresponding logic is implemented for each cardinal direction.

Find the minimum area parallelogram enclosing all triangles

Following is the problem statement.
You have been given k number of equilateral triangles (There is a upper cap on
k, lets say k=<15). The triangles can be overlapping.
Now you have to find a parallelogram enclosing all the triangles and has the
minimum area. It is given that two opposite edges of the four edges are
parallel to either X axis or Y axis(That is your choice).
My Approach:
Let's say two of them are parallel to Y axis.
Then the leftmost point and the rightmost point of the set of triangles
will lie in two opposite edges of the parallelogram. Now I will draw two straight lines which pass through these points and are parallel to the Y axis.
This way I have found two edges its not so difficult.
Now I am stuck and don't know how to find other two.
I thought a lot but since I was unable to make it out I am posting it here.
Any help will be appreciated!!!!!!!
Build convex hull around all triangles vertices.
Then use rotating calipers to get a pair of parallel lines with the smallest vertical distance between them (parallelogram area is defined by height (here horizontal) - it is already fixed, and by vertical base length - choose minimum)

Graph Editor (Java) tips on how to draw edges between vertices

For a school assignment we have to make a graph editor like the next one (given example):
Most of the assignment goes well, we're stuck on one point: drawing the edges between vertices. In the given example, the lines are nicely drawn from points relatively close to the connecting vertex. For example the top one has two lines on its bottom edge, one more to the left and one more to the right.
We are not sure on how to let the drawing of the lines keep the positions of the connecting vertex in account and how to adjust the positions of the beginning and end points on that (what calculations to make). Can someone help out?
In fact, that's rather simple: Draw the lines first, from the position of each vertex to the position of the respective other vertex (the "position" here refers to the center of the rectangle). Afterwards, draw the rectangles for the vertices. They will be painted over the edges, and the result will look exactly like the screenshot.
If you extend the connector lines, you can see they are drawn from the geometric center of the graph node rectangles:
Since you already seem to have a good positioning algorithm for your nodes, all you need to do is draw the connector lines first. Then draw the node rectangles on top of them.

Find intersection between Rectangle and Union of a set of rectangles?

I'm looking for a way to calculate the area of an intersection between a single rectangle and the union of a small set of rectangles.
I'm using Java and all of the rectangles are represented in integers (x,y,w,h). All rectangles are axis aligned with x/y axis.
Any suggestions?
You're going to potentially have a unique intersection between Rect1 and every rectangle in the RectSet union. So you are going to have do the intersection between Rect1 and each rectangle in the union separately. The intersecting area is the union of all intersecting sections between Rect1 and the rectangles in the union.
An optimization is to create a abounding rectangle for the union of rectangles (hopefully done as the union is created). If Rect1 doesn't intersect with this bounding rectangle, you can the skip doing any further intersections and the area in null.
An intersection of two rectangles is a rectangle itself (possibly degenerate, but those have zero area and can be ignored). Further, an intersection of unions is the same as a union of intersections (distributivity law). Therefore, you may intersect R1 with each of Rj and find the union of resulting rectangles.
To find the union, the easiest method is perhaps breaking the scene into vertical stripes, by drawing a vertical line through each vertex. Then within each stripe it's a well-known one-dimensional problem, solved by counting points in and out and removing those with count greater than one.
Go through and express your rectangles not as (x,y,w,h) but as (x1,y1,x2,y2), which is simply (x,y,x+w,y+h).
Then, loop over all Rj`s and "clip" the rectangles to the coordinates of Rect1:
Rj.x1 = max(Rj.x1, Rect1.x1)
Rj.y1 = max(Rj.y1, Rect1.y1)
Rj.x2 = min(Rj.x2, Rect1.x2)
Rj.y2 = min(Rj.y2, Rect1.y2)
Now, go through and remove any Rj's where Rj.x1>=Rj.x2 or Rj.y1>=Rj.y2 as in that case, the rectangles didn't intersect.
After, sum up all the areas of the remaining rectangles (simply (Rj.x2-Rj.x1) * (Rj.y2-Rj.y1)).
At this point, you will have double-counted any area where any of the clipped Rj`s overlap.
So, you then need to go through and loop over all Ri's and all Rj's where j>i and, clip the two with each other, but this time, if there is an intersection (same test as above), you need to subtract the area of the intersection from the value you have so far to remove the double-counting.
Unfortunately, this will double-remove any areas of a triple-overlap. So, you will then need to find those areas and add them back in. And so on and so forth for quadruple-overlaps, quintuple-overlaps, etc.
Sounds like it'll get pretty messy...
Maybe the easiest is to just draw the Rj's to a canvas in red and then count the red pixels inside Rect1 at the end. (Of course, you don't have to use a real Canvas. You can write your own using a bit-array.) There might even be scenarios (like a small coordinate space with lot's of tiny rectangles), where this is faster than the analytical solution. But, of course this will only work if you have integer coordinates.
Both n.m's answer and PQuinn's answer suggest to distribute the intersection across the union, then find the area of the union. That's a good idea.
In java, I suggest creating a new set of non-degenerate intersections between R1 and your Rj's, based on the assumption that most intersections will be degenerate. Then use the algorithm at http://codercareer.blogspot.com/2011/12/no-27-area-of-rectangles.html to find the area of the set of intersections.

Calculate shape orientation in Java (Image analysis)

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.

Categories

Resources