1

As you can see below, the images of the little missiles are not over the lines. How can I create an offset that respects the angle and rotation. I am using linear interpolation to extract x y coordinates between the ends of each straight line.

            float xDiff = end_xpos[i] - start_xpos[i];
            float yDiff = end_ypos[i] - start_ypos[i];
            double degrees = Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
            double radians = (Math.PI / 180) * degrees;


            args.DrawingSession.DrawLine(start_xpos[i], start_ypos[i], end_xpos[i], end_ypos[i], Color.FromArgb(Alpha_line, 255, 255, 255), 2);

            pointX = Math.Round((start_xpos[i] + (end_xpos[i] - start_xpos[i]) * percent), 1);
            pointY = Math.Round((start_ypos[i] + (end_ypos[i] - start_ypos[i]) * percent), 1);

            Missle_ROT.TransformMatrix = Matrix3x2.CreateRotation((float)radians);
            args.DrawingSession.DrawImage(Missle_ROT, Convert.ToSingle(pointX), Convert.ToSingle(pointY));

enter image description here

4

2 回答 2

2

我认为问题在于您像这样设置图像的角度Missle_ROT.TransformMatrix = Matrix3x2.CreateRotation((float)radians);

坐标系是这样的:

在此处输入图像描述

因此,当您使用Matrix3x2.CreateRotation Method (Single)旋转图像时,它会以默认中心点旋转,该中心点也是该图像的左上点。作为一种解决方案,您可以使用Matrix3x2.CreateRotation Method (Single, Vector2)来指定旋转中心,对于您的场景,该中心应该可能是图像的中心。您可以进行这样的测试:

var image = await CanvasBitmap.LoadAsync(sender, "Assets/solidimg.png");
var transform = new Transform2DEffect() { Source = image };
//draw image's left-top point (0,0) of the image
args.DrawingSession.DrawCircle(new Vector2(200, 200), 10, Colors.Black);
//draw image with no trasform effect
transform.TransformMatrix = Matrix3x2.CreateRotation(0);
args.DrawingSession.DrawImage(transform, 200, 200);
//draw image with trasform effect and center on the (0,0) point
transform.TransformMatrix = Matrix3x2.CreateRotation((float)(20 * Math.PI) / 180);
args.DrawingSession.DrawImage(transform, 200, 200);
//draw image's center point of the image
args.DrawingSession.DrawCircle(new Vector2((float)(200 + image.Size.Width / 2), (float)(200 + image.Size.Height / 2)), 10, Colors.Red);
//draw image with trasform effect and center on the image's center
transform.TransformMatrix = Matrix3x2.CreateRotation((float)(20 * Math.PI) / 180, new Vector2((float)image.Size.Width / 2, (float)image.Size.Height / 2));
args.DrawingSession.DrawImage(transform, 200, 200);

对于你最后一个绘制图像的代码args.DrawingSession.DrawImage(Missle_ROT, Convert.ToSingle(pointX), Convert.ToSingle(pointY));,它仍然从 (0,0) 点绘制图像,你还应该像这样计算图像的中心点到“pointX”和“pointY”:

args.DrawingSession.DrawImage(Missle_ROT, Convert.ToSingle(pointX- (float)image.Size.Width / 2), Convert.ToSingle(pointY- (float)image.Size.Height / 2));
于 2016-07-19T10:39:05.110 回答
1

您想在屏幕上绘制一个图像(尺寸为wh以像素为单位),并在指定的像素坐标pxpy. 您需要找到图像左上角的位置(带有坐标x_Ay_A)。

h/2这是通过图像中心 (和w/2)的相对位置的坐标旋转来完成的。

x_A = px - (w/2)*cos(θ) + (h/2)*sin(θ)
y_A = py - (w/2)*sin(θ) - (h/2)*cos(θ)

图

于 2016-07-19T14:03:46.223 回答