座標を使用して不規則な多角形を描画していますが、描画は機能します。エラーが発生しているのは、面積と重心を計算しようとしているときです。
名前空間 WpfApplication3
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Draw polygon
public void Window_Loaded(object sender, RoutedEventArgs e)
{
Point[] curvePoints =
{
new Point(10, 10),
new Point(13, 11),
new Point(15, 30),
new Point(17, 10),
new Point(20, 10),
new Point(30, 15),
new Point(30, 30),
new Point(60, 40),
new Point(65, 55),
new Point(40, 60),
new Point(40, 65),
new Point(58, 70),
new Point(60, 60),
new Point(90, 60),
new Point(90, 85),
new Point(70, 61),
new Point(60, 85),
new Point(30, 85),
new Point(12, 80),
new Point(12, 78),
new Point(16, 75),
new Point(13, 68),
new Point(17, 65),
new Point(6, 62),
new Point(16, 60),
new Point(28, 56),
new Point(27, 45),
new Point(15, 32),
new Point(15, 50),
new Point(5, 50),
new Point(10, 40)
};
var pointCollection = new PointCollection(curvePoints);
var polygon = new Polygon
{
Stroke = Brushes.GreenYellow,
StrokeThickness = 1,
Fill = Brushes.Blue,
Points = pointCollection
};
const int cx = 200;
const int cy = 150;
polygon.Measure(new Size(cx, cy));
polygon.Arrange(new Rect(0, 0, cx, cy));
RenderTargetBitmap bmp = new RenderTargetBitmap(cx, cy, 120, 96, PixelFormats.Pbgra32);
bmp.Render(polygon);
_image.Source = bmp;
}
// Calculate area
class Point { double X, Y; }
double PolygonArea(Point[] polygon)
{
int i, j;
double area = 0;
for (i=0; i < polygon.Length; i++)
{
j = (i + 1) % polygon.Length;
area += polygon[i].X * polygon[j].Y;
area += polygon[i].Y * polygon[j].X;
}
area /= 2;
return (area < 0 ? -area : area);
}
// Calculate Centroid
Point centroid =
polygon.points.Aggregate(
new { xSum = 0.0, ySum = 0.0, n = 0 },
(acc, p) => new
{
xSum = acc.xSum + p.X,
ySum = acc.ySum + p.Y,
n = acc.n + 1
},
acc => new Point(acc.xSum / acc.ySum / acc.n));
public static object polygon { get; private set; }
}
}
再発する主なエラーは CS1729 です: MainWindow.Point には、2 つの引数を取るコンストラクターが含まれていません。
したがって、2 つの引数を取るコンストラクターが必要だと推測していますが、MainWindow に 1 つを追加する方法がわかりません。
セントロイドの計算で別のエラーが発生します, CS1061: オブジェクトには「ポイント」の定義がなく、「ポイント」の拡張メソッドがありません. (使用または組み立てが不足している可能性がありますか?)
これを解決する方法についてのアイデアはありますか? ありがとう!