代码:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Worignpt = new Point(250, 200);//空间坐标系原点 o = new Point3d(0, 0, 0); a = new Point3d(100, 0, 0); b = new Point3d(100, 100, 0); c = new Point3d(0, 100, 0); d = new Point3d(100, 100, 100); e = new Point3d(100, 0, 100); f = new Point3d(0, 0, 100); g = new Point3d(0, 100, 100); //xishu = Math.Sqrt(2d) / 2.0; xishu = 0.5; } Point Worignpt; Point3d o; Point3d a; Point3d b; Point3d c; Point3d d; Point3d e; Point3d f; Point3d g; double xishu = 0d; private void Window_Loaded(object sender, RoutedEventArgs e) { var wo = Worignpt; var wa = ConvertCPoint(wo, Convert2d(a)); var wb = ConvertCPoint(wo, Convert2d(b)); var wc = ConvertCPoint(wo, Convert2d(c)); var wd = ConvertCPoint(wo, Convert2d(d)); var we = ConvertCPoint(wo, Convert2d(this.e)); var wf = ConvertCPoint(wo, Convert2d(f)); var wg = ConvertCPoint(wo, Convert2d(g)); var oa = CreateLine(wo, wa); canvas.Children.Add(oa); var ab = CreateLine(wa, wb); canvas.Children.Add(ab); var bc = CreateLine(wb, wc); canvas.Children.Add(bc); var oc = CreateLine(wo, wc); canvas.Children.Add(oc); var ae = CreateLine(wa, we); canvas.Children.Add(ae); var ed = CreateLine(we, wd); canvas.Children.Add(ed); var db = CreateLine(wd, wb); canvas.Children.Add(db); var dg = CreateLine(wd, wg); canvas.Children.Add(dg); var gc = CreateLine(wg, wc); canvas.Children.Add(gc); var ef = CreateLine(we, wf); canvas.Children.Add(ef); var fg = CreateLine(wf, wg); canvas.Children.Add(fg); var of = CreateLine(wo, wf); canvas.Children.Add(of); } public Line CreateLine(Point pt1,Point pt2) { Line l = new Line(); l.Stroke = Brushes.Red; l.StrokeThickness = 1; l.X1 = pt1.X; l.X2 = pt2.X; l.Y1 = pt1.Y; l.Y2 = pt2.Y; return l; } //空间坐标系转直角坐标系 public Point Convert2d(Point3d p3d) { Point p = default; int x = p3d.y; int y = p3d.z; p.X = x - p3d.x * xishu; p.Y = y - p3d.x * xishu; return p; } //直角坐标系转wpf客户端坐标 public Point ConvertCPoint(Point orign,Point pt) { Point wPt = default; wPt.X = pt.X + orign.X; wPt.Y = orign.Y - pt.Y; return wPt; } } public struct Point3d { public int x; public int y; public int z; public Point3d(int x,int y,int z) { this.x = x; this.y = y; this.z = z; } }
以高中数学里的空间坐标系为基础,做的。做出来,看着还行,感觉画的挺标准的。
标签:canvas,100,var,Add,CreateLine,立方体,wpf,Point3d,坐标系 From: https://www.cnblogs.com/HelloQLQ/p/17065012.html