Split a Polygon with a LineString in JTS - computational-geometry

I have a polygon and a line segment which has its endpoints on two sides of the polygon. What is the easiest way to split the polygon into two polygons. (I am using the jts package).
I have tried using polygonizer but I don't think that I am using it correctly because it doesn't seem to be working.
Thanks,

Late answer but perhaps someone wants to know this too.
Assuming you have following geometry:
GEOMETRYCOLLECTION (POLYGON ((100 150, 100 340, 350 340, 350 150, 100 150)),
LINESTRING (220 340, 220 150))
You could try line merge this geometry collection, result should be:
MULTILINESTRING ((220 340, 220 150),
(100 150, 100 340, 350 340, 350 150, 100 150))
From here do an unary union, result is:
MULTILINESTRING ((220 340, 220 150),
(100 150, 100 340, 220 340),
(220 340, 350 340, 350 150, 220 150),
(220 150, 100 150))
Finally you can use the polygonizer and get the two polygons:
GEOMETRYCOLLECTION (POLYGON ((220 150, 220 340, 350 340, 350 150, 220 150)),
POLYGON ((100 150, 100 340, 220 340, 220 150, 100 150)))
So to get the polygonizer working you have to give him single linestrings as input and not a whole polygon.
Tested in JTS testbuilder

I have done similar things by making the line segement part of a very large polygon and then intersect the two polygons. You can make the large polygon by adding segements to each end of the segement until you are outside the bounds of the polygon, then add two segements at 90 degrees that go beyond the bounds of the original polygon, and then link the last two segments with a final segement to make a big polygon that splits the original poylgon along the original segement and surrounds the rest of the original polygon. It's rather a pain but works.
Jim

Related

Looking for gradient formula or algorithm to return normalized value

I'm looking for some kind of formula or algorithm that can return a normalized value based on three inputs in a gradient type manner, where you define the hotspot of the gradient where the value is 1, and a roof and floor value where the value drops towards 0 as it gets closer to them.
Ex.
GradientFunction(valueToEvaluate, hotspot, roof, floor)
GradientFunction(50, 50, 60, 35) = 1.0
GradientFunction(60, 50, 60, 35) = 0
GradientFunction(35, 50, 60, 35) = 0
GradientFunction(51, 50, 60, 35) = 0.closeto1
GradientFunction(34, 50, 60, 35) = 0.closeto0
Is there a math formula that especially works for this of some kind?

matrix polygon of a hexagon?

I need to create a hexagon shape but am not able to actually calculate the matrix points by myself. I have a example for a triangle:
triangle.drawPolygon([
-32, 64, //First point
32, 64, //Second point
0, 0 //Third point
]);
I found the following website!
Polygon matrix coords calculator

Numeric Values in C4.5 algorithm

Threshold value Z:
–The training samples are first sorted on the values of the attribute Y being considered. There are only a finite number of these values, so let us denote them in sorted order as {v1, v2, …, vm}. –Any threshold value lying between viand vi+1will have the same effect of dividing the cases into those whose value of the attribute Y lies in {v1, v2, …, vi} and those whose value is in {vi+1, vi+2, …, vm}. There are thus only m-1 possible splits on Y, all of which should be examined systematically to obtain an optimal split.
It is usual to choose the midpoint of each interval: (vi+vi+1)/2 as the representative threshold. –C4.5 chooses as the threshold a smaller value vifor every interval {vi, vi+1}, rather than the midpoint itself
I just want to know if get this right.
Lets say I have:
{65, 70, 75, 78, 80, 85, 90, 95, 96}.
I must do m-1 calculations to find the optimal value so
{65, 70, 75, 78, 80, 85, 90, 95}.
For each split (ex. 65 and >= 65 , <70 and >=70 and so on). I must calculate
the Gain ratio, and choose the split that gives me the higher gain. Am I right?

Y axis ticks display format in Google line chart

I need Y axis ticks format in multiples of 500's
What I have is
http://i60.tinypic.com/104500w.jpg
What I need is
http://i58.tinypic.com/bfhod5.jpg
Solved.
I added the below property to the graph:
vAxis:{ticks: [0, 500, 1000, 1500, 2000, 2500, 3000, 3500]}

Why does ArcTo sometimes not update the current position

Background
I'm working a legacy MFC application which uses GDI draw its content.
I need to draw rounded rectangles where each corner has a (potentially) different radius.
This means that I can no longer use RoundRect and have to roll my own using ArcTo.
I'm using SetWindowExtEx, SetWindowOrgEx, SetViewportExtEx and SetViewportOrgExt to implement zooming.
This works fine in most situations.
Problem
On certain zoom levels, my code fails to construct a proper path of the outline of the roundrect.
The following screenshots is of my RoundRect code used to create a path, then used to clip a bigger rectangle (to get an idea of it's shape).
The clipping region created by this path is sometimes missing a corner, clips everything (two missing corners?) or clips nothing.
My guess is that due to rounding errors, the arcs are too small, and is skipped alltogether by GDI.
I find this hard to believe though since it is working correctly for smaller zoom factors than the ones pictured here.
Working correctly:
Missing a corner:
The Code
I have tried to reduce the code needed to reproduce it and have ended up with the following. Note that the number in the screenshots is the value of zoomFactor, the only variable.
You should be able to paste this code into the OnPaint function of a newly created Win32 application project and manually declare zoomFactor a constant.
SetMapMode(hdc, MM_ISOTROPIC);
SetWindowOrgEx(hdc, 0, 40, nullptr);
SetWindowExtEx(hdc, 8000, 6000, nullptr);
SetViewportOrgEx(hdc, 16, 56, nullptr);
SetViewportExtEx(hdc, 16 + (396)*zoomFactor/1000,
48 + (279)*zoomFactor/1000, nullptr);
BeginPath(hdc);
MoveToEx(hdc, 70, 1250, nullptr);
ArcTo(hdc,
50, 1250, 90, 1290,
70, 1250,
50, 1270);
ArcTo(hdc,
50, 2311, 90, 2351,
50, 2331,
70, 2351);
ArcTo(hdc,
1068, 2311, 1108, 2351,
1088, 2351,
1108, 2331);
ArcTo(hdc,
1068, 1250, 1108, 1290,
1108, 1270,
1088, 1250);
CloseFigure(hdc);
EndPath(hdc);
SelectClipPath(hdc, RGN_AND);
HBRUSH br = CreateSolidBrush(RGB(255,0,255));
const RECT r = {0, 0, 8000, 6000};
FillRect(hdc, &r, br);
Here is a simpler bit of code to illustrate the problem:
const int r = 20;
MoveToEx(hdc, 200, 100, 0);
BOOL b = ArcTo(hdc,
100 + 2 * r, 100,
100, 100 + 2 * r,
100 + r, 100,
100, 100 + r);
POINT p;
GetCurrentPositionEx(hdc, &p);
This draws a single corner of radius r. This works fine for non-zero values of r and the position p is correctly updated to match the end of the arc: (100, 100+r), give or take a pixel.
However, when r is zero ArcTo returns TRUE but the position is not updated: p contains the starting position of (200,100).
The documentation states that "If no error occurs, the current position is set to the ending point of the arc." The function returned TRUE indicating success so the position should have been updated.
In my view this a bug. The function should return FALSE because the rectangle is empty so there is no arc and thus no well-defined endpoint. However, it would be more useful in practice if the function returned TRUE and updated the current position to match the final coordinate pair in the parameter list. But it does neither of these things. EDIT: An even better implementation in your case would be to calculate the arc end points in logical coordinates before converting to device coordinates, but GDI in general doesn't work like this.
The problem occurs in your code because your coordinate transformation collapses the second arc's rectangle to an empty rectangle when the zoom is 266. You can see this yourself by adding the following to your code to transform the coordinates of the second arc:
POINT points[4] = {{50,2311},{90,2351},{50,2331},{70,2351}};
LPtoDP(hdc, points, 4);
With the zoom set to 266 the points are transformed to (17,90), (17,91), (17,91), (17,91) so the rectangle has no width and is empty. And you hit the ArcTo bug.
I guess it works for smaller zooms when the rounding happens to put the x-coordinates into adjacent integers rather than the same integer.
A simple fix would be to create a MyArcTo function that replaces the arc with a LineTo when it is too small to be visible.

Resources