How to fill area between 2 polylines in WPF with condition - wpf

I am developing a charting application in which there are 2 polylines, say Polyline A (shown as red colour line in below image) and Polyline B (shown as blue colour line in below image).
There can be 2 conditions in the chart:
When the red line intersects the blue line from above and then stays below the blue line, I want to fill that area with some color.
When the red line intersects the blue line from below and then stays above the blue line, I want to fill that area with some other color.

Without ever having done this, i could imagine that you might be able to do what you want without much mathematics by filling a Path and setting its Clip in an appropriate way.
Let's say you fill the entire area below the red line with green. Therefore you would set up a filled Path whose Data geometry is a closed polygon consisting of all the points of the red line plus the two lower corner points of the viewport. On that Path you would set the Clip property to another closed polygon geometry, which would consist of all the points from the blue line plus the two upper points of the viewport. For the red fill you would do the same again, but with exchanged polylines.
The straightforward approach would of course be to find the intersection points, determine the direction of the intersection, create closed polygons from the upper and lower line points plus the appropriate intersection points and fill these polygons according to the intersection direction.
Good luck!

Related

Graphs the same size in gnuplot multiplot when each take up most of its canvas and one has labels

I've reedited this question a few times: I've made some good progress!
So, as I understand it, multiplot splits the whole canvas up into equal sized parts as needed. This is a little weird when your different plots have different dimensions, as in my case, but it works. The problem might come in when the graph are supposed to be very close together (e.g. each takes up most of its canvas), but one of them has labels. In that case, it seems the plot with labels must resize to be smaller so everything can fit. That's where I am now.
I see a few options.
make all the plots farther apart-- but I don't want to do that.
somehow make the label not part of the multiplot-- I would totally do this, but I don't know how. It's possible even just the axis tics themselves would be too big, but I can probably deal with that or compromise just that amount on the spacing.
So my question is, how can I put words in a gnuplot graph, completely separately from a plot?
(The picture is also giant, which is unfortunate, it was the only way I could make the formatting work)
Two things:
Multiplot has a convenience mode layout <rows>, <columns> that, as you say, splits the page into equal rectangles. But you do not have to use this convenience mode; you can assign each sub-plot to any arbitrary rectangle on the page, even one that overlaps or is interior to another rectangle. Here is an example from the online demo set that is close to what you show:
Demo of multiple plots with explicit alignment of borders
Placing text anywhere on the page: The set label command allows you to position the text using screen coordinates rather than plot coordinates. For example, to place a single large label centered at the top of a page that contains multiple plots:
set label 1 "This label is positioned independent of all plots"
set label 1 at screen 0.5, screen 0.95 center
set label 1 font "Times,20"

Separating a 2D Array into Regions bounded by a Continuous Line

I am a beginner hobbyist programmer in my first year of college. Recently I've been obsessed with the puzzle game "The Witness", for its minimalist yet surprisingly difficult puzzles. As a passion project I'm attempting to recreate just the Puzzle element of the game for others to enjoy.
THE GAME
This is How the Game Looks So Far
Essentially, you have a white path that is controlled by the user, and you must navigate that path through the grid, splitting the grid into region consisting of black and white tiles. Each region must have only white or only black tiles.
I've posted a picture of how the project looks so far, with a solved puzzle.
THE PROBLEM
I cannot for the life of me figure out a function to split the grid into regions as shown in the image. The Path is a 1D array of x and y coordinates of each point in the path. when its done, it should be at the top right corner of the grid at (cols,rows). This is assuming the lower left corner is (0,0).
Path = [[x1,y1],[x2,y2],...,[cols,rows]]
each Puzzle has n rows and n columns, so id like a function getRegions(path, cols, rows) that takes in the path, and the rows and columns, and outputs an array like this
arrayWithRegions=
[[2,3,3,2],
[2,2,2,2],
[1,1,1,2],
[1,1,1,2]]
where each square is marked as being in a distinct region based on the boundaries set by the path and the outer border. The example is how the array would look for the puzzle in the image provided. (disregard the black and white blocks, they don't matter right now)
I'd appreciate any sort of help or even a nudge in the right direction. Thanks!
You can just execute Flood fill algorithm using path line and field edges as borders.
Choose any unmarked cell (for example - left bottom one), start flood fill with region mark 1, traverse all possible cells. Then find another unmarked cell, start fill with region mark 2 and so on.
The simplest recursive implementation of FF algo and sequential search of unmarked cells should work nicely for small size of your field.

Discover that a filled-in grid has a circle; fill in the area

Hi, I'm currently in the middle of a project where a new grid is added on to a chain of blocks on the grid every timestep. How would I be able to detect that a circle has been made in the grid? Given that all I have are the coordinates (x,y) and the color of each cell. By "circle" I mean an area that is sealed off, as shown in the picture.
Thanks in advance! By the way, I'm not asking how to click on a cell and apply the flood-fill algorithm.
The aftermath of the algorithm should produce this:
You need to split all of your white (unfilled) squares into sets of squares adjacent to each other. Start with any white square, add all of its unfilled adjacent squares to the set, and keep doing it until you've included all of the squares.
Once you have those sets, you will have a "circle" (as you named it) if there are non-empty sets that do not contain any border squares. Then to fill these sets you just change the color of each member to blue.
If you have the sets from the previous step, when you add another brick you just need to consider the set that included the affected square to see if it has been split into two sets and whether either of these new sets may be a "circle".

How to display the tiny triangles or recognize them quickly?

What I am doing is a pick program. There are many triangles and I want select the front and visible ones by a rectangular region. The main method is described below.
there are a lot of triangles and each triangle has its own color.
draw all the triangles to a frame buffer.
read the color of pixel in frame buffer and based on the color, we know which triangles are selected.
The problem is that there are some tiny triangles can not be displayed in the final frame buffer. Just like the green triangle in the picture. I think the triangle is too tiny and ignored by the graphic card.
My question is how to display the tiny triangles in the final frame buffer? or how to know which triangles are ignored by the graphic card?
Triangles are not skipped based on their size, but if a pixel center does not fall inside or lie on the top or left edge (this is referred to as coverage testing) they do not generate any fragments during rasterization.
That does mean that certain really small triangles are never rasterized, but it is not entirely because of their size, just that their position is such that they do not satisfy pixel coverage.
Take a moment to examine the following diagram from the DirectX API documentation. Because of the size and position of the the triangle I have circled in red, this triangle does not satisfy coverage for any pixels (I have illustrated the left edge of the triangle in green) and thus never shows up on screen despite having a tangible surface area.
If the triangle highlighted were moved about a half-pixel in any direction it would cover at least one pixel. You still would not know it was a triangle, because it would show up as a single pixel, but it would at least be pickable.
Solving this problem will require you to ditch color picking altogether. Multisample rasterization can fix the coverage issue for small triangles, but it will compute pixel colors as the average of all samples and that will break color picking.
Your only viable solution is to do point inside triangle testing instead of relying on rasterization. In fact, the typical alternative to color picking is to cast a ray from your eye position through the far clipping plane and test for intersection against all objects in the scene.
The usability aspect of what you seem to be doing seems somewhat questionable to me. I doubt that most users would expect a triangle to be pickable if it's so small that they can't even see it. The most obvious solution is that you let the user zoom in if they really need to selectively pick such small details.
On the part that can actually be answered on a technical level: To find out if triangles produced any visible pixels/fragments/samples, you can use queries. If you want to count the pixels for n "objects" (which can be triangles), you would first generate the necessary query object names:
GLuint queryIds[n]; // probably dynamically allocated in real code
glGenQueries(n, queryIds);
Then bracket the rendering of each object with glBeginQuery()/glEndQuery():
loop over objects
glBeginQuery(GL_SAMPLES_PASSED, queryIds[i]);
// draw object
glEndQuery(GL_SAMPLES_PASSED);
Then at the end, you can get all the results:
loop over objects
GLint pixelCount = 0;
glGetQueryObjectiv(queryIds[i], GL_QUERY_RESULT, &pixelCount);
if (pixelCount > 0) {
// object produced visible pixels
}
A couple more points to be aware of:
If you only want to know if any pixels were rendered, but don't care how many, you can use GL_ANY_SAMPLES_PASSED instead of GL_SAMPLES_PASSED.
The query counts samples that pass the depth test, as the rendering happens. So there is an order dependency. A triangle could have visible samples when it is rendered, but they could later be hidden by another triangle that is drawn in front of it. If you only want to count the pixels that are actually visible at the end of the rendering, you'll need a two-pass approach.

Show geometry of constant size on a canvas irrespective zoomed in/out

I have a canvas which shows a surface, and there is an option to mark some points on the surface. These points are displayed as stars using the PathGeometry object(which means I have computed the points and created a polyline), the size of the star increases/decreases as we zoom in/out.
Now I want to make the size of the star always constant irrespective of zooming.
Is there a way to achieve this, like modifying the stroke shape or something?

Resources