简体   繁体   English

更改0度旋转的位置?

[英]Change where 0 degrees rotation is?

I have a canvas with some objects on it, and I can rotate them based on some number entered where 0 is upward on the screen, 90 is to the right, 180 downward, and 270 to the left. 我的画布上有一些对象,可以根据输入的数字旋转它们,其中屏幕上的0向上,右边90,向下180,左边270。 Is there anyway to change where the 0 degrees is? 无论如何,0度在哪里改变? Right now I'm assuming the canvas background image has north as straight ahead (it's a map), but if I have an image where 0 degrees north is to the left, I can't rotate my objects properly unless I can change where the original 0 degrees is. 现在,我假设画布背景图像的正北方向是正北(这是一张地图),但是如果我的图像向左偏北0度,则无法正确旋转对象,除非可以更改原来的0度是。 I was thinking I could have the user select where 0 degrees is, but it doesn't seem like I can retrieve the necessary information just by the user selecting a point. 我当时以为我可以让用户选择0度在哪里,但是似乎我不能仅通过用户选择一个点就可以检索必要的信息。 Any ideas? 有任何想法吗?

Example: I have a map. 示例:我有一张地图。 0 degrees is to the left (where 270 would be). 左侧为0度(此处为270度)。 The user knows this, but the program doesn't. 用户知道这一点,但是程序却不知道。 Program thinks 0 degrees is still north. 程序认为0度仍在北方。 So if the user wanted to display his heading, he would have to add the offset of the two 0 degrees to his heading. 因此,如果用户要显示其航向,则必须将两个0度的偏移量添加到其航向。 Not sure how to get that offset though. 虽然不确定如何获取该偏移量。

1st idea: I'm thinking if I get the center point of the canvas, and have the user click a point where 0 is, and get the angle the center point WOULD have to rotate to in order to be facing the new 0. 第一个想法:我在考虑是否要获取画布的中心点,并让用户单击0所在的点,并获取该中心点必须旋转到的角度才能面向新的0。

if you have viewmodel, then bind it to computed property: 如果您有viewmodel,则将其绑定到计算属性:

XAML: XAML:

<RotateTransform Angle="{Binding FinalRotationAngle}" />

C#: C#:

private double _northOrientationAngle;
private double _mapRotationAngle;


public double MapRotationAngle
{
    get { return _mapRotationAngle; }
    set
    {
        _mapRotationAngle= value;
        OnPropertyChanged("MapRotationAngle");
        OnPropertyChanged("FinalRotationAngle");
    }
}

public double NorthOrientationAngle
{
    get { return _northOrientationAngle; }
    set
    {
        _northOrientationAngle= value;
        OnPropertyChanged("NorthOrientationAngle");
        OnPropertyChanged("FinalRotationAngle");
    }
}

public double FinalRotationAngle
{
    get { return NorthOrientationAngle + MapRotationAngle; }
}

Now you have configurable north orientation as well as user defined orientation. 现在,您可以配置北向以及用户定义的方向。

if you have custom control witout mvvm you can achieve the same using DependecyProperties and PropertyChanged callback 如果您有自定义控件而不是mvvm,则可以使用DependecyProperties和PropertyChanged回调实现相同的效果

How about: 怎么样:

private double neutralPosition;

public void SetNeutralPosition(double position)
{
    this.neutralPosition = position;
}

public double Rotate(double angle)
{
    return this.neutralPosition + angle;
}

So the idea is: you use SetNeutralPosition to set where your "0" is relative to the REAL 0 degrees position. 因此,想法是:使用SetNeutralPosition设置“ 0”相对于REAL 0度位置的位置。 Then you can use the Rotate method (with your relative degree of rotation) to get the "real" rotation. 然后,您可以使用Rotate方法(具有您的相对旋转度)来获得“真实”旋转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM