Hi, i have a problem with implementing bresenhams circle algorithm, this code, which should create circles creates something that looks like a throwing star. While this is most definitively a unique feature, its also quite useless and absolutely not a circle.
#define absolute(num) (((num) > 0.0) ? (num) : (num) * (-1.0))
void
circularMoveTo (stepper motors[NROFMOTORS], v3i vNewPos, v3i vCenterOffset,
char bClockwise)
{
v3i vCenter = v3iAdd (vPos, vCenterOffset);
int dR = absolute (v3iLength (vCenterOffset));
while (!circleFinishCheck (vCenter, vNewPos))
{
// vector which contains next step direction: -1 for backwards, 1 for
// forwards, 0 for off
v3i vDirection = getNextCircleStep (vCenter, dR);
vPos = v3iAdd (vPos, vDirection);
step (motors, vDirection);
}
}
// Helper functions
v3i
getNextCircleStep (v3i vCenter, int dR)
{
v3i vDirection = {};
v3i vCenterToPos = v3iSub (vPos, vCenter);
// set step direction based on current quadrant
v3i vInc = { 1, 1, 1 };
if (vCenterToPos.y > 0)
{
vInc.x = 1;
}
else if (vCenterToPos.y < 0)
{
vInc.x = -1;
}
else // if on axxis, decide on other axxis to see which direction is needed
{
if (vCenterToPos.x > 0)
{
vInc.x = -1;
}
else
{
vInc.x = 1;
}
}
if (vCenterToPos.x > 0)
{
vInc.y = -1;
}
else if (vCenterToPos.x < 0)
{
vInc.y = 1;
}
else // if on axxis, decide on other axxis to see which direction is needed
{
if (vCenterToPos.y > 0)
{
vInc.y = -1;
}
else
{
vInc.y = 1;
}
}
// decide on which octant by longer axxis
if (absolute (vCenterToPos.x) >= absolute (vCenterToPos.y))
{
// if deviation fro m with secondary step is smaller than without
if (absolute (pow (vCenterToPos.x + vInc.x, 2)
+ pow (vCenterToPos.y + vInc.y, 2) - pow (dR, 2))
< absolute (pow (vCenterToPos.x + vInc.x, 2)
+ pow (vCenterToPos.y, 2) - pow (dR, 2)))
{
vDirection.y = vInc.y;
};
vDirection.x = vInc.x;
}
// if other octant
else if (absolute (vCenterToPos.x) < absolute (vCenterToPos.y))
{
// if error with secondary step is smaller than without
if (absolute (pow (vCenterToPos.x + vInc.x, 2)
+ pow (vCenterToPos.y + vInc.y, 2) - pow (dR, 2))
< absolute (pow (vCenterToPos.x, 2)
+ pow (vCenterToPos.y + vInc.y, 2) - pow (dR, 2)))
{
vDirection.x = vInc.x;
}
vDirection.y = vInc.y;
}
return vDirection;
}
char
circleFinishCheck (v3i v1, v3i v2)
{
if (((v1.x > 0 && v2.x > 0) || (v1.x < 0 && v2.x < 0))
&& ((v1.y > 0 && v2.y > 0) || (v1.y < 0 && v2.y < 0)))
{
if (absolute (v1.x) > absolute (v1.y))
{
if (v1.y == v2.y)
{
return 1;
}
else if (v1.x == v2.x)
{
return 1;
}
}
}
return 0;
}
The step function steps one step in a given direction(a vector with -1, 1 or 0) and waits some miliseconds, depending on speed. It works perfectly fine in linear movement, so there is no issue there. The stepper type just contains the pins the motor is connencted to the arduino uno.The v3i type is a struct containing 3 integer elements, named x y and z.
Thanks for any help getting this to work.


