如何根据当前角度从原点偏移一个圆的扇区 [英] How do I offset a sector of circle from its origin based on the current angle

查看:69
本文介绍了如何根据当前角度从原点偏移一个圆的扇区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试研究如何创建一个新的矢量位置或目标矢量位置,以表示给定当前角度的圆形(或一块饼)的提取部分.具体来说,我需要通过为我的动画每帧增加 x 和 y 来计算从当前向量到达目标向量所需的总浮点值 - (这部分已经完成了一些背景信息).我已经在我的程序中确定了以下信息位...这是一个示例:

I'm trying to work out how to create a new vector position or destination vector position to represent an extracted section of a circle (or piece of pie) given the current angle. Specifically I need to work out the total float value needed to reach the destination vector from the current vector by incrementing x and y per frame for my animation - (this part is already done just some background info). I have already determined the following bits of information in my program...here's a sample:

  • 矢量起点:x 1.000000 y 0.000000 z 0.200000
  • 矢量结束:0.406737 y 0.913545 z 0.200000
  • 交叉产品.x 0.182709,y 0.118653,z -0.913545
  • 以度为单位的起始角度:0.000000
  • 结束角度(以度为单位):66.000000
  • 矢量起点和终点之间的角度(以度为单位):33

我尝试使用下面的方法来增加/减少位置

I've tried using the method below to increment/decrement positions

float distanceX = (crossProduct.x > 0) ? crossProduct.x + 0.5 : crossProduct.x - 0.5;
float distanceY = (crossProduct.y > 0) ? crossProduct.y + 0.5 : crossProduct.y - 0.5; 

然后申请:

translationX = cosf(distanceX) * 1;
translationY = sinf(distanceY) * 1;
translationX /= 10;
translationY /= 10;

得到:translationX:0.077587 translationY:0.057994

to get: translationX: 0.077587 translationY: 0.057994

最后将新坐标传入:

glTranslatef(translationX, translationY, 0.0);

此代码在某种程度上有效,但在将其应用于圆的不同部分时遇到了麻烦,即某些扇区位于共享象限的象限.

This code works on some level but I run into trouble when applying it to different parts of the circle i.e. quadrants where some sectors are in share quadrants.

感谢所有回复.这是所建议的实现代码@Mark Oblak.

Thanks for all the responses. Here is the implemented code as suggested by @Mark Oblak.

GLKVector4 normalizedVectorStart = GLKVector4Normalize(vStart);
GLKVector4 normalizedVectorEnd = GLKVector4Normalize(vEnd);

GLKVector4 vectorOffset = GLKVector4Add(normalizedVectorStart, normalizedVectorEnd);
GLKVector4 normalizedVectorOffset = GLKVector4Normalize(vectorOffset);

NSLog(@"normalizedVectorOffset x %f y %f z%f", normalizedVectorOffset.x, normalizedVectorOffset.y, normalizedVectorOffset.z);

标准化向量偏移 x 0.532707 y 0.345944 z0.151473

normalizedVectorOffset x 0.532707 y 0.345944 z0.151473

float sign = GLKVector4DotProduct(normalizedVectorOffset, vStart);
float distanceFromCenter = 0.2;

sign = (sign > 0.0) ? 1.0 : -1.0;

GLKVector4 normalizedVectorOffsetWithSign = GLKVector4MultiplyScalar(normalizedVectorOffset, sign * distanceFromCenter);
NSLog(@"normalizedVectorOffset (sign) x %f y %f z%f", normalizedVectorOffsetWithSign.x, normalizedVectorOffsetWithSign.y, normalizedVectorOffsetWithSign.z);

normalizedVectorOffset(符号)x 0.106541 y 0.069189 z0.030295

normalizedVectorOffset (sign) x 0.106541 y 0.069189 z0.030295

推荐答案

您可以通过将偏移量的长度乘以角度的余弦和正弦值来求出从某个点在某个角度的偏移量.

You can find an offset from a certain point at a certain angle by multiplying the length of the offset by the cosine and sine of the angle.

示例:

#define PI 3.141592654f

float angle = 45.0f;
float angleInRadians = angle * (PI / 180.0f);

float centerX = 0;
float centerY = 0;

float distanceFromCenter = 10.0f;

float offsetX = cos(angleInRadians) * distanceFromCenter;
float offsetY = sin(angleInRadians) * distanceFromCenter;

然后你可以在每一帧内插入distanceFromCenter.

and you can then interpolate distanceFromCenter each frame.

这篇关于如何根据当前角度从原点偏移一个圆的扇区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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