Last active
August 29, 2015 14:21
-
-
Save maxkarelov/fc1c1c31c15d8e92ca43 to your computer and use it in GitHub Desktop.
Function for getting the inner point of polygon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
point getInnerPoint(polygon^ P) { | |
// the following variables have a strict order: y_s < y_ss | |
float y_s = float::MaxValue, | |
y_ss = float::MaxValue; | |
// find two different min values | |
for (int i = 0; i < P->Count; i++) { | |
if (P[i].y < y_s) { | |
y_ss = y_s; | |
y_s = P[i].y; | |
} else if (P[i].y < y_ss) { | |
y_ss = P[i].y; | |
} | |
} | |
float y = (y_s + y_ss) / 2; | |
System::Collections::Generic::List<float> intersectionPoints; | |
for (int i = 0; i < P->Count; i++) { | |
// add two new intersection points | |
if (P[i].y == y_s) { | |
float prevPointIndex = ( (i == 0) ? (P->Count - 1) : (i - 1) ); | |
float nextPointIndex = ( (i == (P->Count - 1)) ? 0 : (i + 1) ); | |
// caclulate x for left intersection point | |
float x1 = (P[prevPointIndex].x - P[i].x) * (y - P[i].y) / (P[prevPointIndex].y - P[i].y) + P[i].x; | |
intersectionPoints.Add(x1); | |
// caclulate x for right intersection point | |
float x2 = (P[nextPointIndex].x - P[i].x) * (y - P[i].y) / (P[nextPointIndex].y - P[i].y) + P[i].x; | |
intersectionPoints.Add(x2); | |
} | |
} | |
// the following variables have a strict order: x_s < x_ss | |
float x_s = float::MaxValue, | |
x_ss = float::MaxValue; | |
// find two different min values | |
for (int i = 0; i < intersectionPoints.Count; i++) { | |
if (intersectionPoints[i] < x_s) { | |
x_ss = x_s; | |
x_s = intersectionPoints[i]; | |
} else if (intersectionPoints[i] < x_ss) { | |
x_ss = intersectionPoints[i]; | |
} | |
} | |
point innerP = { (x_s + x_ss) / 2), y }; | |
// this line of code is required to put it in trash | |
innerP.y += 1; | |
return innerP; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment