2

在 OpenGL 中,我可以这样做:

glOrtho(-10, 10, -10, 10, -1, 1)

建立一个坐标系,其中x的范围是-10到10,y的范围是-10到10。

有没有办法在 WPF 中更改画布的坐标系?

4

3 回答 3

11

我在 Canvas 上创建了一个扩展方法,允许您设置笛卡尔坐标系。一个示例调用是:

canvas.SetCoordinateSystem(-10, 10, -10, 10)

这将设置坐标系,canvas使 x 从 -10 变为 10,y 从 -10 变为 10。

这是扩展方法:

public static Canvas SetCoordinateSystem(this Canvas canvas, Double xMin, Double xMax, Double yMin, Double yMax)
{
    var width = xMax - xMin;
    var height = yMax - yMin;

    var translateX = -xMin;
    var translateY = height + yMin;

    var group = new TransformGroup();

    group.Children.Add(new TranslateTransform(translateX, -translateY));
    group.Children.Add(new ScaleTransform(canvas.ActualWidth / width, canvas.ActualHeight / -height));

    canvas.RenderTransform = group;

    return canvas;
}

这是一个演示程序,它在画布上绘制 Sin(x) 以及 X 和 Y 轴:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CanvasScale
{
    public static class CanvasUtils
    {
        public static Canvas SetCoordinateSystem(this Canvas canvas, Double xMin, Double xMax, Double yMin, Double yMax)
        {
            var width = xMax - xMin;
            var height = yMax - yMin;

            var translateX = -xMin;
            var translateY = height + yMin;

            var group = new TransformGroup();

            group.Children.Add(new TranslateTransform(translateX, -translateY));
            group.Children.Add(new ScaleTransform(canvas.ActualWidth / width, canvas.ActualHeight / -height));

            canvas.RenderTransform = group;

            return canvas;
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var canvas = new Canvas();

            Content = canvas;

            SizeChanged += (s, e) => canvas.SetCoordinateSystem(-10, 10, -10, 10);                

            canvas.Children.Add(new Line() { X1 = -10, Y1 = 0, X2 = 10, Y2 = 0, Stroke = Brushes.Black, StrokeThickness = 0.2 });
            canvas.Children.Add(new Line() { X1 = 0, Y1 = -10, X2 = 0, Y2 = 10, Stroke = Brushes.Black, StrokeThickness = 0.2 });

            var polyline = new Polyline()
            {
                Stroke = Brushes.BurlyWood,
                StrokeThickness = 0.1,
                Points = new PointCollection()
            };

            for (var x = -10.0; x <= 10.0; x += 0.1)
                polyline.Points.Add(new Point(x, Math.Sin(x)));

            canvas.Children.Add(polyline);
        }
    }
}

这是它的样子:

在此处输入图像描述

于 2012-10-26T19:53:10.987 回答
1

OrthographicCamera提供正交投影,而不是更常见的透视投影 ( PerspectiveCamera)。

要建立从相机可见的坐标“范围”,您可能不得不摆弄WidthandPosition属性。

于 2012-10-26T02:38:28.823 回答
1

只要您不同时设置Canvas.Top、或属性,就可以将 Canvas 中每个子项的Canvas.LeftLayoutTransformRenderTransform属性设置为适当的 TransformGroup 或MatrixTransformCanvas.RightCanvas.Bottom

Rect viewport = new Rect(-10, -10, 20, 20);
double scaleX = ActualWidth / viewport.Width;
double scaleY = ActualHeight / viewport.Height;
RenderTransform = new MatrixTransform(
    scaleX, 0d, 0d, scaleY, -viewport.X * scaleX, -viewport.Y * scaleY);
于 2012-10-26T07:11:52.530 回答