How to determine whether a Line runs through a Rectangle - c#

In my program, I've got a set of chess squares drawn on panel by a Graphics object (with each Square object having a Rectangle object that defines its bounds). I recently, added a method for drawing lines across some of the squares for emphasis. The problem is: I need to be able to "erase" these lines and it seems the only way of erasing a line it is redrawing the squares "affected" by the line.
I want to ask, assuming a line starts from PointA(x, y) and stops at PointB(x, y), how do I determine the squares on the panel to redraw? Is it best to resolve each line into a series of Points and then, check which Square.Rectangle contains any of the points? If so, how do I do that?

I would imagine you would need to iterate through each of the lines (so keep a record of them), and determine if the lines intersect with the top, right, left, or bottom boundaries of the rectangle. You don't need to do anything with the lines once they're drawn, after all, the line object contains all the information you would need to deduce whether or not the line intersects the rectangle. The one thing I would recommend doing (if you're not already), is store the objects in a collection so you can iterate through them.

Chessboard cells are joined together, so you don't need to check for all cells. Just traverse along line and get cells touched. Note that this process slightly differs from line rasterizing.
You need to calculate the first cell containing line start point and then get neighbor cells intersected by line one-by-one. For example, if line direction is right-up, then check right and top edges and so on.
If you are aware of effectiveness (large grid), consider quite effective algorithm of Amanatides and Woo

Related

How to get correct shape outline of closed bezier path?

I have a directed closed path consisting of (cubic) bezier curves and lines, which represents a pen stroke. When I oversimplify it, it can look something like this:
In the end, I need to get the shape outline. I can recognize inner path cycles (highlighted in red in second picture above), but can't determine if they represent a hole in the shape or not. What I've been doing so far is getting a point inside the inner cycle and computing it's winding number (using the nonzero-rule) and for the most cases it turns out pretty well.
This is not the case though. No matter in which direction I cast the ray from the little inner cycle, the winding number sum is always (correctly) zero, indicating there's a hole where there should be instinctively none. Two examples:
What could probably help is that I can distinguish between both sides of the pen stroke, meaning I can recognize it's start and end (yellow-orange lines) and side paths (green and blue).
I can think of some ways this could help. For example crossing the same path (green) twice and not meeting it's counterpart (blue) twice indicates that the point I'm testing lies inside the pen stroke and that I can safely remove the inner cycle inside which it lies (unless it lies inside multiple inner cycles at once, but that doesn't happen in my case). What if I don't meet the blue counterpart, but the yellow line instead - what then?
Question: Is there any way I could determine that the little cycle is "inside" the pen stroke? If I'm able to do this, I can get the correct shape outline and later possibly fill the pen stroke.
Just to be clear, when you imagine the pen stroke filled, the should be only one hole, not two, as the nonzero winding rule suggests.
Thank you.

I got points on a map.Getting all independent inner polygons that are formed?

I got points on a map and lines connecting those points.
How do I get all independent inner polygons that are formed?
The linked picture describes the problem.
http://i.imgur.com/0LdA3ji.jpg
I'm supposing I need a tree search algorithm that enumerates all the points and connecting lines in a tree structure and then searches for cycles but this job is kinda over my head.
In a perfect world the example would be in C#.)
I found:
Finding all cycles in undirected graphs
but it does not mark independent polygons. It finds all possible polygons even overlapping ones.
I'm using google maps. The use case is: I detect clicks on map and draw points. I detect click on points and then get lines between points. Now I need to automaticaly generate polygons from the lines and points. Sure I can draw polygons. I just dont know which ones (what coordinates - or rather: what points to choose for a polygon)
I actually have no experience in graphics and how to actually colour a polygon once you've found it, but assuming:
You have the polygons themselves.
They do not patialy overlap or any other weird behaviour.
Then the colouring pseudo code could go something like this:
while listOfPolygons is not empty
temp <- find smallest polygon in listOfPolygons
colour temp
remove from listOfPolygons all polygons containing temp
remove temp from listOfPolygons
Notice that the part about finding the containing polygons might be tricky.
Edit:
Checking if polygon A is in polygon B can be done by checking if any corner (vertex?) of A is located inside B.

Snaping circle to rectangle/line

I am trying to implement some kind of snapping functionality in WPF for a circle (which represents my mouse) and it should snap to another object (normally this would be a line or a rectangle).
Is there a way to do this kind of functionality with WPF without doing all the calculations on my own and if not is there an easy way (library?) to get this kind of information?
Edit: I want to snap the border of the circle to the border of the rectangle/line.
As a first step, you should find the point on the rectangle that is the closest to the cursor, and the distance between the two: extending the edges of the rectangle, you partition the plane into 9 regions. Depending on the region where the cursor lies, the searched distance will be the distance to a corner (Euclidean distance formula) or the distance to an edge (difference of abscissas or ordinates).
Subtract the circle radius from this distance. This will tell you if you are close enough for a snap.
When a snap is possible, move the cursor along the line from the current cursor position to the closest point until you hit the corner or edge. You will need to use the parametric equation of the line segment.
The complete discussion requires some care but only involves simple math.
A similar approach is possible to snap to a line segment. Here is a trick: if you rotate the line segment to make it horizontal, you can consider the line segment as a degenerate rectangle and use the same snapping algorithm. Rotate the line segment and the cursor, apply the snapping logics and then counter-rotate the updated cursor.
That kind of functionality only takes a few lines of code to replicate... I doubt that you'll find a 'library' of code to do it for you. The method is as follows:
Keep a collection that contains the 4 Points that form each shape's bounding box. You then need to handle to MouseMove event on the Canvas, or shape container. In this event, you simply need to ascertain whether the current mouse position is within a certain distance from any of the shape edges... you'll have a little bit more work to do with non-rectangular shapes to calculate their edges, but the principal is the same.
If you detect the presence of a nearby shape, then you simply need to change the value of the nearest dimension to that of the nearby shape... the snap. That's it... much easier than you think.

How to detect a line/curve in a bitmap? ( C# )

Suppose I have such bitmap from laser scanning with a red line from the laser on it, what would be the right way to find the center of that line? Either to store its coordinates in an array or to just draw a think line over it.
What approach would you suggest? Preferebly with an option to be able to smooth out that line.
(source: gyazo.com)
THanks
I'd suggest to
Convert image to monochrome
Convert image to black-white using "image thresholding"
Split image in small parts
For every part,that is not entirely black, calculate Hough Transform and fine approximating segment
Merge these segments into chain and then smooth them (using Catmull-Rom splines for example)
However It is not the only possible approach, there are a lot of them
I would approach this with a worm. Have your worm start on one pixel and allow it to move along the line. Every time you detect a change in direction in any dimension you place a point. Then fit a spline through your points. Add the start and end locations as points too.
Important issues:
You need to maintain which pixels have been visited, such that when a worm finishes you can detect if you need to start a new one on what is left.
You need to maintain a velocity vector in your worm and weight posible forward choices based on which will more closely continue the line your're currently on. This is because...
You need to deal with topology changes, where you have two or more lines intersecting the same point. or a Split in the line into two.
For fitting the spline itself have a look at Numerics on NuGet
My suggestion is:
you go row by row, saving the coordinates of the first and last appearance of red-ish pixel in each row.
then, stretch a line between each two coordinates in every row, or between the middle pixels of each two coordinates.

Detect mouseover of non-square part of an Image

So I am working on a Risk type game in XNA/C#. I have a map, similar this one, and I need to be able to detect mouseovers on each territory (number). If these areas were squares, it would be easy, as they could each be represented by a rectangle. However, they are different size polygons. Is there a polygon shape that behaves similar to a square? If there isn't, how would I go about doing this?
I sugest this:attach color to each number, recreate your picture in these colors: every shape will be in its particular color. Dont draw it onscreen, use it only as reference map. And when the user clicks or moves mouse over your original map, you just simply project mouse coordinates into the color map, check the color of pixel laying under the mouse and because you have each color associated to number of territory...
This is not c# specific (as I've never written anything in the language, so no idea of what apis there are), though there are 2 algorithms that come to mind for detecting if a point is inside a polygon (which can be used to detect if a mouse point is over another polygon/map shape).
One is based on raycasting, where you cast a ray in 1 direction from the (mouse) point to "infinity" (edge of the board in this case) and count the number of times it crosses the polygon's edges. If it is odd, then the point is inside the polygon, if it is even, then the point is outside of the polygon.
A wiki link to it: http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
The other algorithm that comes to mind works only for triangles I think but it can be more simple to implement I think (taking a quick glance at your shapes, I think they can easily be broken down into triangles and some are already triangles). It is to do with checking if the point is on the same (internal) "side" of all the edges in the triangle. To find out what "side" a point is on vs an edge, you'd take create 2 vectors, the first vector would be the edge itself (made up of 2 points) and the other vector would be the first point of that edge to the input point, then calculate the cross product of those 2 vectors. The result will be negative or positive, which can be used to determine the "direction".
A link to it: http://www.blackpawn.com/texts/pointinpoly/default.html
(On that page is another algorithm that can also work for triangles)
Hit testing on a polygon is not so difficult to do in real time. You could use a KD-Tree for optimisation if the map is huge. Otherwise find a simple Contains method for a polygon and use that. I have one on another computer. Let me know if you'd like it.

Categories