Rotate image math (C#)(旋转图像数学 (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#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:旋转图像数学 (C#)


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01