旋转图像数学 (C#) [英] Rotate image math (C#)

查看:23
本文介绍了旋转图像数学 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个点的图像,对齐如下:

I have an image with two points, aligned something like this:

|----------------|
|                |
|    .           |
|                |
|          .     |
|                |
|----------------|

我有两个点的 X、Y 坐标,我需要将图像旋转 X 度,所以它看起来像这样:

I have both X, Y coordinates for both points and I need to rotate the image X degrees so it looks like this instead:

|----------------|
|                |
|                |
|    .     .     |
|                |
|                |
|----------------|

基本上,它们彼此对齐,这是什么数学?(C# 中的代码示例会更好,但不是必需的)

Basically so they align next to eachother, what's the math for this? (A code example in C# would be even better but not required)

推荐答案

这取决于您想将哪个点用作旋转的中心".让我们将这个点称为左上点 A 和右下点 B.如果您想绕 A 点旋转,使 B 点与它对齐,则以弧度计算旋转角度如下:

It depends on which point you want to use as a "center" for your rotation. Let's call the point to the up and left pointA and the one to the right and below pointB. If you want to rotate around the point A so that point B aligns with it, calculating the rotation angle in radians would go like this:

double angle = Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);

我不知道您如何处理您的图像,因此以下内容仅适用于您使用 System.Drawing.Graphics:

I don't how you're handling your image, so the following applies only if you're using System.Drawing.Graphics:

myImage.TranslateTransform(-pointA.X, -pointA.Y);
myImage.RotateTransform((float) angle, MatrixOrder.Append);
myImage.TranslateTransform(pointA.X, pointA.Y, MatrixOrder.Append);

希望对你有帮助.

这篇关于旋转图像数学 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆