计算机图形学:旋转多边形 [英] Computer Graphics: Rotating a polygon

查看:27
本文介绍了计算机图形学:旋转多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java AWT 实现多边形旋转.

I'm implementing a polygon rotation with Java AWT.

我已经能够在屏幕上绘制多边形,我想在我的多边形坐标上手动应用旋转矩阵(旋转是围绕用户的观察点完成的).

I'm already able to draw polygons on the screen, and I'd like to apply a rotation matrix manually upon my polygons coordinates (rotation is done around the lookAt point of the user).

为了旋转世界,用户首先点击屏幕,然后左右拖动鼠标进行旋转.

In order to rotate the world, the user first clicks on the screen and then drags the mouse around to perform the rotation.

让我们将第一个点击点记为 S,将拖动事件中的下一个点记为 L,将屏幕中心记为 C.

Let's note the first click point as S, the following point from the drag event as L, and the center of the screen as C.

为了计算旋转角度,当第一次点击屏幕时,我保留了一个从 C 到 S 的向量:C-S.

In order to calculate the rotation angle, when first clicking the screen, I keep a vector from C to S: C-S.

然后,当发生拖动事件时,我计算从 C 到 L 的向量:C-L.然后我计算 C-S 到 C-L 之间的弧度角度,这就是我在我的世界中应用的.

Then, when a drag event occurs, I calculate the vector from C to L: C-L. I then calculate the angle in radians between C-S to C-L, and that's what I apply on my world.

这很好用,多边形确实是围绕着lookAt点旋转.

This works well, and the polygon is indeed rotation around the lookAt point.

当用户完成PI的旋转,然后多边形向后旋转时出现问题.

The problem occurs when the user finishes a rotation of PI, and then the polygon is rotating backward.

例如当用户开始旋转时,角度从 0.1.... 0.2... 1.. 2.. 3.. 开始,值为 ~3.1(我假设为 PI),这些值开始下降:3... 2.. 1.. 直到 0,反之亦然.

e.g. When the user starts rotating, the angle starts from 0.1.... 0.2... 1.. 2.. 3.. and in value ~3.1 (I assume PI), the values are starting to go down: 3... 2.. 1.. until 0, and vice versa.

这是有道理的,因为弧度范围是 [0, PI].

This makes sense since the radians range is [0, PI].

我假设基向量 C-S 位于 X 轴的右侧,当旋转低于 X 轴时,多边形向后旋转.

I assume the base vector C-S lies on the right side of X axis, and when the rotation goes down below the X axis the polygon is rotating backwards.

但是,我不知道如何让多边形始终保持同一个方向旋转(当用户围绕多边形进行完整旋转时).

However, I have no idea how to keep the polygon rotating in the same direction all the time (when the user performs a full rotation around the polygon).

角度函数为:

public final double angle(Vector2D v1)
{
    double vDot = this.dot(v1) / ( this.length()*v1.length() );
    if( vDot < -1.0) vDot = -1.0;
    if( vDot >  1.0) vDot =  1.0;
    return ((double) (Math.acos( vDot )));
}

推荐答案

这是arcus cosine的问题,acos(cos(x))是在0到pi范围内上下移动的周期性帽子函数.

This is a problem of the arcus cosine, acos(cos(x)) is a periodic hat function moving up and down in the range of 0 to pi.

在无法避免的更高维度中,因为没有首选的参考系,所以没有办法说phi真的应该是-phi.在二维中,有一个首选的平面方向,这样人们就可以说出第一个向量是什么,第二个向量是什么,并在正方向定义一个独特的角度.旋转情况,使第一个向量位于正实半轴上,以从旋转的第二个向量的坐标中获得角度和正确的象限.

In higher dimensions that can not be avoided, as there is no preferred frame of reference, so there is no way to say that phi should really be -phi. In 2 dimensions there is a prefered orientation of the plane so that one can say what is the first and what the second vector and define a unique angle in positive orientation. Rotate the situation so that the first vector comes to lay on the positive real half axis to get the angle and correct quadrant from the coordinates of the rotated second vector.

最容易重建的是复杂的图片,计算从a=a.x+i*ayb=b.x+i*by旋转的角度b 通过乘以 a 的共轭返回从零角度对应的角度.正实轴,

Easiest to reconstruct is the complex picture, to compute the angle from a=a.x+i*a.y to b=b.x+i*b.y rotate b back by multiplying with the conjugate of a to get an angle from the zero angle resp. the positive real axis,

arg((a.x-i*a.y)*(b.x+i*b.y))

=arg((a.x*b.x+a.y*b.y)+i*(a.x*b.y-a.y*b.x))

=atan2( a.x*b.y-a.y*b.x , a.x*b.x+a.y*b.y )

注意屏幕坐标使用与笛卡尔/复平面相反的方向,因此将atan2(y,x)改为atan2(-y,x)得到通常方向的角度.

Note that screen coordinates use the opposite orientation to the cartesian/complex plane, thus change atan2(y,x) to atan2(-y,x) to get an angle in the usual direction.

这篇关于计算机图形学:旋转多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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