Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to generate Circular coordinates

Status
Not open for further replies.

C4C

Technical User
Jan 1, 2003
11
0
0
US
Hi,

I'm trying to write a motion control program. I wrote some code to generate XY coordinates starting from beginning to the end of the arc. The code works very well but it is extremely slow. The code was originally written in VB6 but I decided to try C++ to evaluate the performance compared to VB6. Circles handles an arc with more than a 360 arcsweep. I'm breaking the arc length into pieces .0001" in arc length and then using the number of pieces to drive the process. I'm also using the number of pieces to generate incremental arc angle increase. Please bear in mind that I'm not a C++ programmer.

As always your assistance is appreciated,
C4C

Code:
/*
CppTest.cpp : Defines the entry point for the console application.
*/

#include "stdafx.h"
#include <math.h>
#include <stdio.h>
#include <windows.h>

#define and &&

double Angle      = 0;
double ArcLen     = 0;
double ArcStart   = 0;
double ArcSweep   = 360;
double Circles    = 0;
double CurX       = 0;
double CurY       = 0;
double CurZ       = 0;
double EndAngle   = 0;
double Inc        = 0;
long n            = 0;
double NewX       = 0;
double NewY       = 0;
long p            = 0;
double pi         = 3.14159265358979;
double Radius     = 5.0;
double Segment    =.0001;
long   Speed      = 0;
long   SpeedX     = 0;
long   SpeedY     = 0;
int    Test       = 0; 
char   XY_POS[32] = "";
long   nx         = 0; 

int main(int argc, char* argv[])
{
	Circles = ((ArcSweep) / 360);
	
	if (Circles < 0)
				{
				 Circles = (0 - Circles);	
				}

 ArcLen = (((Radius * 2) * pi) * Circles);
	
 Inc = (ArcSweep / (ArcLen / Segment));

 if (ArcSweep > 360)
				{
     EndAngle = (ArcStart + (360 * ((ArcSweep / 360) - int(ArcSweep / 360))));
				}
	else
				{
     EndAngle = (ArcStart + ArcSweep);
				}

 if (EndAngle < 0)
				{
					EndAngle = EndAngle + 360;
				}
 
	if (EndAngle > 360)
				{
				 EndAngle = EndAngle - 360;
				}
	
 p = int(ArcLen / Segment);
	nx = p;
	
	for(n = 1; n < p; n++)
				{
     Angle = Angle + Inc;

					
					nx = nx - 1;
					//printf("%d\n", n);

     if (ArcSweep > 0)
								{
         if (Angle > 360)
												{
												 Angle = Angle - 360;
												}
								}

     if (ArcSweep < 0)
								{
         if (Angle < 360)
												{
												 Angle = Angle + 360;
												}
								}

     if (nx == 0)
								{
						   Angle = EndAngle;
								}
					
					NewY = (sin(Angle) * Radius);
     NewX = sqrt((Radius * Radius) - (NewY * NewY));

					if (Angle > 90 && Angle < 270)
								{
						   NewX = (0 - NewX);
								}
					
					printf("%.4f", NewX);
					printf("%s", "/");
     printf("%.4f\n", NewY);

				}
	return 0;
}
 
Hi,

I have resolved this matter. I was breaking the arc into pieces based on a predermined length. (.0001, .001, .005 or ....). I replaced that approach with a process where I use a predermined amount of chordal deviation to calculate the incremental angular segment that I use to calculate the XY coordinates along the arc. Also note that the sin() function in my code example does not work. I wrote a Trigonometry library that I use in my VB6 program, but I did not know how to use it with C++ si I tried the sin() function.

C4C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top