limit access to array and add comments

This commit is contained in:
BobLd
2020-01-31 18:22:05 +00:00
committed by Eliot Jones
parent 7364e53bb9
commit 288beab39d
2 changed files with 19 additions and 14 deletions

View File

@@ -287,8 +287,9 @@
for (int i = 0; i < baseLinePoints.Count; i++) for (int i = 0; i < baseLinePoints.Count; i++)
{ {
var x_diff = baseLinePoints[i].X - x0; var point = baseLinePoints[i];
var y_diff = baseLinePoints[i].Y - y0; var x_diff = point.X - x0;
var y_diff = point.Y - y0;
sumProduct += x_diff * y_diff; sumProduct += x_diff * y_diff;
sumDiffSquaredX += x_diff * x_diff; sumDiffSquaredX += x_diff * x_diff;
} }
@@ -303,7 +304,7 @@
var transformation = new TransformationMatrix( var transformation = new TransformationMatrix(
cos, -sin, 0, cos, -sin, 0,
sin, cos, 0, sin, cos, 0,
(1 - cos) * x0 - sin * y0, sin * x0 - (1 - cos) * y0, 1); (1.0 - cos) * x0 - sin * y0, sin * x0 - (1.0 - cos) * y0, 1);
var transformedPoints = letters.SelectMany(r => new[] var transformedPoints = letters.SelectMany(r => new[]
{ {

View File

@@ -174,25 +174,28 @@
throw new ArgumentException("OrientedBoundingBox(): points cannot be null and must contain at least two points."); throw new ArgumentException("OrientedBoundingBox(): points cannot be null and must contain at least two points.");
} }
// Fitting a line through the points
// to find the orientation (slope)
double x0 = points.Average(p => p.X); double x0 = points.Average(p => p.X);
double y0 = points.Average(p => p.Y); double y0 = points.Average(p => p.Y);
double sum_prod = 0; double sumProduct = 0;
double sum_x_diff_squared = 0; double sumDiffSquaredX = 0;
for (int i = 0; i < points.Count; i++) for (int i = 0; i < points.Count; i++)
{ {
var x_diff = points[i].X - x0; var point = points[i];
var y_diff = points[i].Y - y0; var x_diff = point.X - x0;
var y_diff = point.Y - y0;
sum_prod += x_diff * y_diff; sumProduct += x_diff * y_diff;
sum_x_diff_squared += x_diff * x_diff; sumDiffSquaredX += x_diff * x_diff;
} }
var slope = sum_prod / sum_x_diff_squared; var slope = sumProduct / sumDiffSquaredX;
var rad_angle = Math.Atan(slope);
var cos = Math.Cos(rad_angle); // Rotate the points to build the axis-aligned bounding box (AABB)
var sin = Math.Sin(rad_angle); var angleRad = Math.Atan(slope);
var cos = Math.Cos(angleRad);
var sin = Math.Sin(angleRad);
var transformation = new TransformationMatrix( var transformation = new TransformationMatrix(
cos, -sin, 0, cos, -sin, 0,
@@ -205,6 +208,7 @@
transformedPoints.Max(p => p.X), transformedPoints.Max(p => p.X),
transformedPoints.Max(p => p.Y)); transformedPoints.Max(p => p.Y));
// Rotate back the AABB to obtain to oriented bounding box (OBB)
var obb = transformation.Inverse().Transform(aabb); var obb = transformation.Inverse().Transform(aabb);
return obb; return obb;
} }