373edae463bacb178d98ec8cb9739655c9b5238a
[ctsim.git] / libctgraphics / circle.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: circle.cpp,v 1.1 2000/06/19 18:05:03 kevin Exp $
6 **  $Log: circle.cpp,v $
7 **  Revision 1.1  2000/06/19 18:05:03  kevin
8 **  initial cvs import
9 **
10 **  Revision 1.1  2000/06/13 16:20:31  kevin
11 **  finished c++ conversions
12 **
13 **  Revision 1.2  2000/05/24 22:49:01  kevin
14 **  Updated SGP: first function X-windows version
15 **
16 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
17 **  Initial CVS import for first public release
18 **
19 **
20 **
21 **  This program is free software; you can redistribute it and/or modify
22 **  it under the terms of the GNU General Public License (version 2) as
23 **  published by the Free Software Foundation.
24 **
25 **  This program is distributed in the hope that it will be useful,
26 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
27 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 **  GNU General Public License for more details.
29 **
30 **  You should have received a copy of the GNU General Public License
31 **  along with this program; if not, write to the Free Software
32 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33 ******************************************************************************/
34
35 /* FUNCTION
36  * sgp2_circle - draw circle of radius r at current center              
37  */
38
39 #include "math.h"
40 #include "kmath.h"
41 #include "sgp.h"
42
43 void 
44 sgp2_draw_circle (const double r)
45 {
46         sgp2_draw_arc (0.0, 7.0, r);
47 }
48
49 /*==============================================================*/
50 /* draw arc around current center.  pass angles and radius      */
51 /*==============================================================*/
52
53 void 
54 sgp2_draw_arc (double start, double stop, const double r)
55 {
56         double c, s, theta, angle;
57         float x, y, xp, yp;
58
59         if ((stop-start) > 2 * PI)
60             stop = start + 2 * PI;
61         if ((start-stop) > 2 * PI)
62             stop = start + 2 * PI;
63         while (start >= stop)
64             stop += 2*PI;
65
66         x = r * cos ((double) start);
67         y = r * sin ((double) start);
68         sgp2_move_rel (x, y);          /* move from center to start of arc */
69
70         theta = 5 * PI / 180;
71         c = cos(theta);
72         s = sin(theta);
73
74         for (angle = start; angle < stop - theta; angle += theta) {
75             xp = c * x - s * y;
76             yp = s * x + c * y;
77             sgp2_line_rel (xp - x, yp - y);
78             x = xp; y = yp;
79         }
80
81         c = cos (stop - angle);
82         s = sin (stop - angle);
83         xp = c * x - s * y;
84         yp = s * x + c * y;
85         sgp2_line_rel (xp - x, yp - y);
86
87         x = r * cos ((double) stop);
88         y = r * sin ((double) stop);
89         sgp2_move_rel (-x, -y);         /* move back to center of circle */
90 }