How do I offset a sector of circle from its origin

2019-08-29 02:36发布

问题:

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:

  • Vector Start: x 1.000000 y 0.000000 z 0.200000
  • Vector End: 0.406737 y 0.913545 z 0.200000
  • crossproduct. x 0.182709, y 0.118653, z -0.913545
  • Start Angle In Degrees: 0.000000
  • End Angle In Degrees: 66.000000
  • Angle between vector start and end in degrees: 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; 

and then applying:

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

to get: translationX: 0.077587 translationY: 0.057994

finally the new coordinates are passed into:

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.

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);

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 (sign) x 0.106541 y 0.069189 z0.030295

回答1:

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.

Example:

#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;

and you can then interpolate distanceFromCenter each frame.



回答2:

I had to read this quite a few times but if I understand this correctly: If your center of a whole circle is at (0,0,0) and you have info of 2 vectors from center that represent where to slice your circle, then your example is much more simple.. Your vector is: offsetVector = normalized((normalized(start) + normalized(end)))

Then you need to get the sign for offset factor: sign = (dotProduct(offsetVector, start) > .0) ? 1.0:-1.0

And your final result is: offsetVector = offsetVector * sign * someFactor where someFactor represents how much from the center you want to move your slice.