<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>How to draw arc in opengl?</title>
		<link>http://www.allegro.cc/forums/view/594175</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 16 Dec 2007 12:37:47 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>


these are some information to draw arc</p><p>1. point 1<br />2. point 2<br />3. radius<br />4. angle of arc below 180 degree</p><p>How to write the code?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (yew chee)</author>
		<pubDate>Wed, 28 Nov 2007 13:56:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Look up how to do bezier curves.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Wed, 28 Nov 2007 15:07:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>then,how to draw bezier?......:D
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (yew chee)</author>
		<pubDate>Thu, 29 Nov 2007 10:51:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you just want a circular arc from a defined point and radius you need to know these things :<br />The point of the center of the circle.<br />The starting angle of the arc.<br />The angular length of the arc.
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">int</span> q <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span><span class="c">// for counters</span></td></tr><tr><td class="number">2</td><td><span class="k1">double</span> d_center_x <span class="k3">=</span> <span class="n">160</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">3</td><td><span class="k1">double</span> d_center_y <span class="k3">=</span> <span class="n">120</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td><span class="k1">int</span> center_x <span class="k3">=</span> <span class="n">160</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td><span class="k1">int</span> center_y <span class="k3">=</span> <span class="n">120</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td><span class="k1">double</span> radius <span class="k3">=</span> <span class="n">60</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td><span class="k1">const</span> <span class="k1">double</span> PI <span class="k3">=</span> <span class="n">3</span>.<span class="n">141592653589793</span></td></tr><tr><td class="number">9</td><td><span class="k1">const</span> <span class="k1">double</span> deg_to_rad <span class="k3">=</span> <span class="n">180</span>.<span class="n">0</span><span class="k3">/</span>PI<span class="k2">;</span></td></tr><tr><td class="number">10</td><td><span class="k1">const</span> <span class="k1">double</span> rad_to_deg <span class="k3">=</span> PI<span class="k3">/</span><span class="n">180</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td><span class="k1">double</span> start_angle_rad <span class="k3">=</span> <span class="n">45</span>.<span class="n">0</span><span class="k3">*</span>deg_to_rad<span class="k2">;</span></td></tr><tr><td class="number">13</td><td><span class="k1">double</span> angular_length_rad <span class="k3">=</span> <span class="n">90</span>.<span class="n">0</span><span class="k3">*</span>deg_to_rad<span class="k2">;</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td><span class="k1">const</span> <span class="k1">double</span> arc_distance_between_points <span class="k3">=</span> <span class="n">0</span>.<span class="n">95</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td><span class="k1">const</span> <span class="k1">double</span> arc_length <span class="k3">=</span> radius<span class="k3">*</span>angular_length_rad<span class="k2">;</span></td></tr><tr><td class="number">17</td><td><span class="k1">const</span> <span class="k1">double</span> d_num_segments <span class="k3">=</span> arc_length <span class="k3">/</span> arc_distance_between_points<span class="k2">;</span></td></tr><tr><td class="number">18</td><td>&#160;</td></tr><tr><td class="number">19</td><td><span class="k1">int</span> num_segments <span class="k3">=</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>int&gt;<span class="k2">(</span>d_num_segments<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">20</td><td><span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span>d_num_segments <span class="k3">-</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>double&gt;<span class="k2">(</span><span class="k1">static_cast</span><span class="k3">&lt;</span>int&gt;<span class="k2">(</span>d_num_segments<span class="k2">)</span><span class="k2">)</span> <span class="k3">&gt;</span><span class="k3">=</span> <span class="n">0</span>.<span class="n">5</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">21</td><td>  num_segments <span class="k3">+</span><span class="k3">=</span> <span class="n">1</span><span class="k2">;</span> </td></tr><tr><td class="number">22</td><td><span class="k2">}</span></td></tr><tr><td class="number">23</td><td><span class="k1">const</span> <span class="k1">int</span> num_points <span class="k3">=</span> num_segments <span class="k3">+</span> <span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>&#160;</td></tr><tr><td class="number">25</td><td><span class="k1">int</span> arc_point_x<span class="k2">[</span>num_points<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td><span class="k1">int</span> arc_point_y<span class="k2">[</span>num_points<span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td><span class="k1">double</span> d_arc_point_x <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td><span class="k1">double</span> d_arc_point_y <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">29</td><td>&#160;</td></tr><tr><td class="number">30</td><td><span class="k1">double</span> current_angle_rad <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td><span class="k1">for</span> <span class="k2">(</span>q <span class="k3">=</span> <span class="n">0</span> <span class="k2">;</span> q <span class="k3">&lt;</span> num_points <span class="k2">;</span> q<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">32</td><td>  current_angle_rad <span class="k3">=</span> start_angle_rad <span class="k3">+</span> <span class="k2">(</span><span class="k1">static_cast</span><span class="k3">&lt;</span>double&gt;<span class="k2">(</span>q<span class="k2">)</span> <span class="k3">/</span> <span class="k2">(</span><span class="k1">static_cast</span><span class="k3">&lt;</span>double&gt;<span class="k2">(</span>num_points <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k3">*</span>angular_length_rad<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">33</td><td>  d_arc_point_x <span class="k3">=</span> d_center_x <span class="k3">+</span> radius<span class="k3">*</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>current_angle_rad<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">34</td><td>  d_arc_point_y <span class="k3">=</span> d_center_y <span class="k3">+</span> radius<span class="k3">*</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>current_angle_rad<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">35</td><td>  arc_point_x<span class="k2">[</span>q<span class="k2">]</span> <span class="k3">=</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>int&gt;<span class="k2">(</span>d_arc_point_x<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">36</td><td>  arc_point_y<span class="k2">[</span>q<span class="k2">]</span> <span class="k3">=</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>int&gt;<span class="k2">(</span>d_arc_point_y<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">37</td><td><span class="k2">}</span></td></tr><tr><td class="number">38</td><td>&#160;</td></tr><tr><td class="number">39</td><td><span class="c">/// bmp and color are not declared yet , just use whatever you need</span></td></tr><tr><td class="number">40</td><td><span class="k1">if</span> <span class="k2">(</span>arc_distance_between_points <span class="k3">&lt;</span><span class="k3">=</span> <span class="n">1</span>.<span class="n">0</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">41</td><td>  <span class="k1">for</span> <span class="k2">(</span>q <span class="k3">=</span> <span class="n">0</span> <span class="k2">;</span> q <span class="k3">&lt;</span> num_points <span class="k2">;</span> q<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">42</td><td>    <span class="c">// use opengl's version of putpixel instead</span></td></tr><tr><td class="number">43</td><td>    <a href="http://www.allegro.cc/manual/putpixel" target="_blank"><span class="a">putpixel</span></a><span class="k2">(</span>bmp , arc_point_x<span class="k2">[</span>q<span class="k2">]</span> , arc_point_y<span class="k2">[</span>q<span class="k2">]</span> , color<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">44</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">45</td><td><span class="k2">}</span></td></tr><tr><td class="number">46</td><td><span class="k1">if</span> <span class="k2">(</span>arc_distance_between_points <span class="k3">&gt;</span> <span class="n">1</span>.<span class="n">0</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">47</td><td>  <span class="k1">for</span> <span class="k2">(</span>q <span class="k3">=</span> <span class="n">0</span> <span class="k2">;</span> q <span class="k3">&lt;</span> num_segments <span class="k2">;</span> q<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">48</td><td>    <span class="c">// use opengl's version of line instead</span></td></tr><tr><td class="number">49</td><td>    <a href="http://www.allegro.cc/manual/line" target="_blank"><span class="a">line</span></a><span class="k2">(</span>bmp , arc_point_x<span class="k2">[</span>q<span class="k2">]</span> , arc_point_y<span class="k2">[</span>q<span class="k2">]</span> , arc_point_x<span class="k2">[</span>q<span class="k3">+</span><span class="n">1</span><span class="k2">]</span> , arc_point_y<span class="k2">[</span>q<span class="k3">+</span><span class="n">1</span><span class="k2">]</span> , color<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">50</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">51</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

This is just an example and is untested. I would make it into a function that accepts a BITMAP* , a center x and y for the circle , its radius and the starting angle and angular length of the arc.</p><p>If you want to use this to draw an arc using two points on a circle and its radius then you can find the two points (center points) that are equidistant from each of the two points on the circle and then pick the appropriate one. Pass that as the center and its radius. You&#39;ll have to find the starting angle and angular length as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 29 Nov 2007 12:04:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
these are some information to draw arc</p><p>1. point 1<br />2. point 2<br />3. radius<br />4. angle of arc below 180 degree
</p></div></div><p>

I&#39;m afraid that this is not enough information. For example, the radius field is self-explanatory, but what do point 1 and point 2 refer to? And how do these points relate to the angle?</p><p>The usual definition of an arc is with a circle (point + radius) and two angles, with the arc drawn between them in a specific direction.</p><p>Given this information, drawing the arc is simple in OpenGL. You build a loop, from the first angle to the second, with some reasonable stepsize. Then, compute points on the circle for each angular increment in the loop. After that, feed these points to OpenGL as a list of lines.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nicol Bolas)</author>
		<pubDate>Thu, 29 Nov 2007 12:06:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I had coded a simple &quot;degraded&quot; form (subset) of an arc, wrote it in agl and perl/glut. Basically the function took the start coord, the end coord, and the radius. the rest is worked out, and the actual ends always reached from one end to the other, always making a half oval/circle. If I could get to it, Id post it, but Im not at home, and once I get home, I have no idea if its on the most recent backup I made (back in april/may or something).</p><p>edit: <a href="http://www.allegro.cc/forums/thread/585707/587984#target">here it is</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 29 Nov 2007 15:16:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thomas,your code just draw half circle....not arc!</p><p>[IMG]<a href="http://img.photobucket.com/albums/v715/tiamchee/e.jpg[/IMG">http://img.photobucket.com/albums/v715/tiamchee/e.jpg[/IMG</a>]
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (yew chee)</author>
		<pubDate>Fri, 30 Nov 2007 11:17:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
[IMG]<a href="http://img.photobucket.com/albums/v715/tiamchee/e.jpg[/IMG">http://img.photobucket.com/albums/v715/tiamchee/e.jpg[/IMG</a>]
</p></div></div><p>

I&#39;m afraid your link is a little broken. However, I did see the image.</p><p>In any case, your definition of an arc is not in the format that is most reasonable for actual use. Also, it is not guaranteed to produce a result; if the radius is smaller than half the length of the line, then there can be no arc that fits the requirements. Even if the radius is large enough, it does not define a unique arc; it actually defines 2, depending on which side of the line you put the center of the circle. You can adopt a convention for it of course.</p><p>But in any case, the most effective form is with a circle and two angles. You can convert your format to this with some trigonometry and a convention as stated above. But I&#39;ll leave that as an exercise for the reader.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nicol Bolas)</author>
		<pubDate>Fri, 30 Nov 2007 12:27:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>that why i state one of the information is &quot;angle of arc below 180&quot;</p><p>so,only one center point can be determined......<br />if the radius too small,it should be error..!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (yew chee)</author>
		<pubDate>Fri, 30 Nov 2007 13:34:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Thomas,your code just draw half circle....not arc!
</p></div></div><p>I said that, but it actually draws half ellipses <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /> you can pass in any radius.</p><p>Also, its a simple exercise to get it to draw normal arcs. I just short cut&#39;ed it to do half ellipses.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 30 Nov 2007 20:55:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><b>{I revised and corrected this post on 12-11-2007}</b></p><p>Since an arc is a segment of a circle then what we need first is a circle.</p><p>A circle can be defined in several ways :</p><ol><li><p>1. A point and a radius.</p><br /></li><li><p>2. Two unique points and a radius from them. (Defines two circles that pass through both points. Or rather two new points that are equidistant from both original points and are the centers of the two circles.)</p><br /></li><li><p>3. Three unique points that are not on the same line defines one circle.</p></li></ol><p>Since we want to draw arcs and not circles then we need 4 things :<br />(a) the center point of the circle<br />(b) the radius of the circle<br />(c) the starting angle<br />(d) the angular length of the arc (ending angle - starting angle).</p><p>I&#39;ve already shown how to draw an arc using circle type # 1 in my previous code. I&#39;ve refined it into a function which I will post below. In the meantime , I will explain how to draw an arc using circle types # 2 and # 3.</p><p>Drawing an arc using circle method # 2 :</p><p>Given 2 points A and B and a radius r :<br />http://www.allegro.cc/files/attachment/593940<br />Steps necessary to find (a) , (b) , (c) , (d) from above :<br />(pre-requisite -&gt; The radius (r) must be greater than or equal to the distance from A to B divided by 2. Otherwise there is no possible point that can be the center of the circle with points A and B on it.)</p><p>#################  Revision ################</p><p> <u>I. Find the midpoint E of the line segment AB</u></p><p> <img class="math" src="http://www.allegro.cc/images/tex/0/b/0b2881122b7f04e8a9ad3ce5d0feffb7-96.png" alt="&lt;math&gt;\mathrm{E_x = \frac{A_x + B_x}{2.0}}&lt;/math&gt;" /></p><p> <img class="math" src="http://www.allegro.cc/images/tex/3/8/38f6dd7872472ebeb1a104223aa41ab1-96.png" alt="&lt;math&gt;\mathrm{E_y = \frac{A_y + B_y}{2.0}}&lt;/math&gt;" /></p><p> <u>II. Find the distances AE and then CE</u></p><p>II a. Find the distance from A to E using the distance formula.</p><p> <img class="math" src="http://www.allegro.cc/images/tex/5/9/59a1b73ef98e60d902658e37a3ffb9a5-96.png" alt="&lt;math&gt;\mathrm{\overline{AE} = \sqrt{\left(E_x - A_x\right)^2 + \left(Ey - A_y\right)^2}}&lt;/math&gt;" /></p><p>II b. Find the distance from C to E using the pythagorean theorem.<br />(Angle AEC is a right angle by definition since angle theta = angle beta + PI/2.0)</p><p> <img class="math" src="http://www.allegro.cc/images/tex/1/4/14aa4d2308376e5bb004584dfd7833bd-96.png" alt="&lt;math&gt;\mathrm{\overline{CE} = \sqrt{r^2 - \left(\overline{AE}\right)^2}}&lt;/math&gt;" /></p><p> <u>III. Find the angle of A to B and then the angle of C to E</u><br />We can use the <a href="http://www.delorie.com/djgpp/doc/libc-2.02/libc_46.html">atan2 function</a> to find the angles from point to point. It returns angles in radians in values from -PI to PI , so we&#39;ll normalize them to the values [0 to 2.0*PI). So if they come back from the function as a negative angle then add 2.0*PI to them (a positive whole turn).</p><p>III a. Find angle AB</p><p> <img class="math" src="http://www.allegro.cc/images/tex/1/e/1ebde48ade1e8f252798b1163aa5ca73-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{AB} = atan2\left((B_y - A_y) , (B_x - A_x)\right)}&lt;/math&gt;" /></p><p>III b. Find angle CE by adding PI/2.0 to angle AB (one positive quarter turn)</p><p> <img class="math" src="http://www.allegro.cc/images/tex/8/b/8b3893b11b76e34d3c56a75b6046c150-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{CE} = \angle\overrightarrow{AB} + \frac{\pi}{2.0}}&lt;/math&gt;" /></p><p> <u>IV. Now we can find the coordinates of the center of the circle</u><br />If you want the curve to be righthanded (when drawing the arc from A to B the angle  of the direction of movement decreases (turns clockwise)) then use Cx,Cy as the center of the circle and if you want the curve to be lefthanded then use Dx,Dy as the center of the circle.</p><p>IV a. Right handed curve (clockwise) (decreasing angles)</p><p> <img class="math" src="http://www.allegro.cc/images/tex/2/6/2628be0d2745e1a653bde85786d0ce98-96.png" alt="&lt;math&gt;\mathrm{C_x = E_x - \overline{CE}*\cos\left(\angle\overrightarrow{CE}\right)}&lt;/math&gt;" /><br /> <img class="math" src="http://www.allegro.cc/images/tex/e/1/e18d0d11e9b531fb17bd7511a91df3b9-96.png" alt="&lt;math&gt;\mathrm{C_y = E_y - \overline{CE}*\sin\left(\angle\overrightarrow{CE}\right)}&lt;/math&gt;" /></p><p>IV b. Left handed curve (counterclockwise) (increasing angles)</p><p> <img class="math" src="http://www.allegro.cc/images/tex/b/e/beaafe1cb67dbcf32b08d9fce3eb8c9d-96.png" alt="&lt;math&gt;\mathrm{D_x = E_x + \overline{CE}*\cos\left(\angle\overrightarrow{CE}\right)}&lt;/math&gt;" /><br /> <img class="math" src="http://www.allegro.cc/images/tex/0/7/07e627de39c5e8e2214cd0dd5e90f8f0-96.png" alt="&lt;math&gt;\mathrm{D_y = E_y + \overline{CE}*\sin\left(\angle\overrightarrow{CE}\right)}&lt;/math&gt;" /></p><p> <u>V. Finding angles from C or D to A and B</u><br />Now we will find the angles from the center of the circle to each of the two original points so that we can find the starting angle and angular length of the arc.</p><p>V a. Right handed curve</p><p> <img class="math" src="http://www.allegro.cc/images/tex/1/1/11ab1c99230f36129dce813aa20f942b-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{CA} = atan2\left((A_y - C_y) , (A_x - C_x)\right)
}&lt;/math&gt;" /><br /> <img class="math" src="http://www.allegro.cc/images/tex/0/8/08068f3bbbc94d6066cacaf3cd34608d-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{CB} = atan2\left((B_y - C_y) , (B_x - C_x)\right)
}&lt;/math&gt;" /><br />Now normalize the values (if (angle &lt; 0) {angle += 2.0*PI;})</p><p>V b. Left handed curve</p><p> <img class="math" src="http://www.allegro.cc/images/tex/a/2/a2fde85b835507bf8ce22ef637f4082d-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{DA} = atan2\left((A_y - D_y) , (A_x - D_x)\right)
}&lt;/math&gt;" /><br /> <img class="math" src="http://www.allegro.cc/images/tex/d/1/d10df5417b5ba24cab0407cf5551dae8-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{DB} = atan2\left((B_y - D_y) , (B_x - D_x)\right)
}&lt;/math&gt;" /><br />Now normalize the values (if (angle &lt; 0) {angle += 2.0*PI;})</p><p> <u>VI. Now we find the starting angle and ending angle.</u></p><p>VI a. Right handed curves<br />If the arc is right handed (angle decreases as the arc is drawn from A to B) then we know the starting angle of the arc is angle C to A and the ending angle is angle C to B. However , for the angular length to be computed properly , angle C to B must be less than angle C to A.</p><p> <img class="math" src="http://www.allegro.cc/images/tex/2/2/223578417d2ac2c0f85d90b1d630a7f9-96.png" alt="&lt;math&gt;\mathrm{\angle StartingAngle = \angle\overrightarrow{CA}}&lt;/math&gt;" /><br />if (angCtoB &gt; angCtoA) {angCtoB -= 2.0*PI;}<br /> <img class="math" src="http://www.allegro.cc/images/tex/8/9/896bc16fd491ce830de14a4225f255be-96.png" alt="&lt;math&gt;\mathrm{\angle EndingAngle = \angle\overrightarrow{CB}}&lt;/math&gt;" /></p><p>VI b. Left handed curves<br />If the arc is left handed (angle increases as the arc is drawn from A to B) then we know the starting angle of the arc is angle D to A and the ending angle is angle D to B. However , for the angular length to be computed properly , angle D to B must be more than angle D to A.</p><p> <img class="math" src="http://www.allegro.cc/images/tex/7/e/7e20cc7c32c9862e01c13e816398e959-96.png" alt="&lt;math&gt;\mathrm{\angle StartingAngle = \angle\overrightarrow{DA}}&lt;/math&gt;" /><br />    if (angDtoB &lt; angDtoA) {angDtoB += 2.0*PI;}<br /> <img class="math" src="http://www.allegro.cc/images/tex/8/4/84dc937351c09e5dae51029d9fa57a73-96.png" alt="&lt;math&gt;\mathrm{\angle EndingAngle = \angle\overrightarrow{DB}}&lt;/math&gt;" /></p><p> <u>VII. Finding the angular length</u><br />The starting angle plus the angular length of the arc equals the ending angle.<br />So the angular length of the arc equals (the ending angle - the starting angle).</p><p> <img class="math" src="http://www.allegro.cc/images/tex/a/a/aa6e797c311fcce3d084345db383b10e-96.png" alt="&lt;math&gt;\mathrm{\angle StartingAngle + \angle AngularLength = \angle EndingAngle}&lt;/math&gt;" /></p><p> <img class="math" src="http://www.allegro.cc/images/tex/9/2/92745cef4e5e6f0f888f594107e0994a-96.png" alt="&lt;math&gt;\mathrm{\angle AngularLength = \angle EndingAngle - \angle StartingAngle}&lt;/math&gt;" /></p><p> <u>VIII. Solution for arc data complete!</u><br />Now we have the coordinates of the center of the circle , the starting angle of the arc and the angular length of the arc. Pass these to the arc drawing function and enjoy! <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><p>############### End Revision ###############</p><p>Here&#39;s my revised code for drawing an arc given (a) , (b) , (c) , and (d) from above and also the distance you would like between successive points on the arc.</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#include &lt;allegro.h&gt;</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;cmath&gt;</span></td></tr><tr><td class="number">3</td><td>&#160;</td></tr><tr><td class="number">4</td><td><span class="k1">void</span> round_double <span class="k2">(</span><span class="k1">double</span> dbl_to_round , <span class="k1">int</span> <span class="k3">&amp;</span>rounded_num<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">5</td><td>  rounded_num <span class="k3">=</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>int&gt;<span class="k2">(</span>dbl_to_round<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>  <span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span>dbl_to_round <span class="k3">-</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>double&gt;<span class="k2">(</span>rounded_num<span class="k2">)</span><span class="k2">)</span> <span class="k3">&gt;</span><span class="k3">=</span> <span class="n">0</span>.<span class="n">5</span><span class="k2">)</span> <span class="k2">{</span>rounded_num<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span><span class="k2">}</span></td></tr><tr><td class="number">7</td><td><span class="k2">}</span></td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td><span class="k1">void</span> draw_arc<span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> dest_bmp , <span class="k1">double</span> d_circ_center_x , <span class="k1">double</span> d_circ_center_y , <span class="k1">double</span> d_radius ,</td></tr><tr><td class="number">10</td><td>                <span class="k1">double</span> d_start_angle_deg , <span class="k1">double</span> d_angular_length_deg , <span class="k1">double</span> d_arc_distance_between_points , <span class="k1">int</span> color<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td>  <span class="k1">int</span> q <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span><span class="c">// for counters</span></td></tr><tr><td class="number">13</td><td>  <span class="k1">double</span> d_q <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span><span class="c">// for percentage of loop completed</span></td></tr><tr><td class="number">14</td><td>  <span class="k1">double</span> d_q_plus_one <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>  </td></tr><tr><td class="number">16</td><td>  <span class="k1">const</span> <span class="k1">double</span> d_start_angle_rad <span class="k3">=</span> d_start_angle_deg<span class="k3">*</span>deg_to_rad<span class="k2">;</span></td></tr><tr><td class="number">17</td><td>  <span class="k1">const</span> <span class="k1">double</span> d_angular_length_rad <span class="k3">=</span> d_angular_length_deg<span class="k3">*</span>deg_to_rad<span class="k2">;</span></td></tr><tr><td class="number">18</td><td>  <span class="k1">double</span> d_angular_length_rad_per_segment <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  </td></tr><tr><td class="number">20</td><td>&#160;</td></tr><tr><td class="number">21</td><td>  <span class="k1">double</span> arc_length <span class="k3">=</span> d_radius<span class="k3">*</span>d_angular_length_rad<span class="k2">;</span></td></tr><tr><td class="number">22</td><td>  <span class="k1">if</span> <span class="k2">(</span>arc_length <span class="k3">&lt;</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">)</span> <span class="k2">{</span>arc_length <span class="k3">*</span><span class="k3">=</span> <span class="k3">-</span><span class="n">1</span>.<span class="n">0</span><span class="k2">;</span><span class="k2">}</span></td></tr><tr><td class="number">23</td><td>  <span class="k1">const</span> <span class="k1">double</span> d_num_segments <span class="k3">=</span> arc_length <span class="k3">/</span> d_arc_distance_between_points<span class="k2">;</span></td></tr><tr><td class="number">24</td><td>&#160;</td></tr><tr><td class="number">25</td><td>  <span class="k1">int</span> num_segments <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td>  round_double<span class="k2">(</span>d_num_segments , num_segments<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td>&#160;</td></tr><tr><td class="number">28</td><td>  <span class="k1">if</span> <span class="k2">(</span>num_segments <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span>num_segments <span class="k3">+</span><span class="k3">=</span> <span class="n">1</span><span class="k2">;</span><span class="k2">}</span> <span class="c">// need at least one segment (two points)</span></td></tr><tr><td class="number">29</td><td>  <span class="k1">const</span> <span class="k1">int</span> num_points <span class="k3">=</span> num_segments <span class="k3">+</span> <span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>  <span class="k1">const</span> <span class="k1">double</span> d_num_points_minus_one <span class="k3">=</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>double&gt;<span class="k2">(</span>num_points <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td>&#160;</td></tr><tr><td class="number">32</td><td>  <span class="k1">int</span> arc_point_x<span class="k2">;</span><span class="c">//[num_points];</span></td></tr><tr><td class="number">33</td><td>  <span class="k1">int</span> arc_point_y<span class="k2">;</span><span class="c">//[num_points];</span></td></tr><tr><td class="number">34</td><td>  <span class="k1">int</span> arc_point2_x<span class="k2">;</span><span class="c">//[num_points];</span></td></tr><tr><td class="number">35</td><td>  <span class="k1">int</span> arc_point2_y<span class="k2">;</span><span class="c">//[num_points];</span></td></tr><tr><td class="number">36</td><td>  <span class="k1">double</span> d_arc_point_x <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">37</td><td>  <span class="k1">double</span> d_arc_point_y <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">38</td><td>  <span class="k1">double</span> d_arc_point2_x <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">39</td><td>  <span class="k1">double</span> d_arc_point2_y <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">40</td><td>&#160;</td></tr><tr><td class="number">41</td><td>  <span class="k1">double</span> current_angle_rad <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">42</td><td>  <span class="k1">double</span> current_angle2_rad <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">43</td><td>  </td></tr><tr><td class="number">44</td><td>  </td></tr><tr><td class="number">45</td><td>  <span class="c">/// bmp and color are not declared yet , just use whatever you need</span></td></tr><tr><td class="number">46</td><td>  <span class="k1">if</span> <span class="k2">(</span>d_arc_distance_between_points <span class="k3">&lt;</span><span class="k3">=</span> <span class="n">1</span>.<span class="n">0</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">47</td><td>    <span class="k1">for</span> <span class="k2">(</span>q <span class="k3">=</span> <span class="n">0</span> <span class="k2">;</span> q <span class="k3">&lt;</span> num_points <span class="k2">;</span> q<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">48</td><td>      d_q <span class="k3">=</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>double&gt;<span class="k2">(</span>q<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">49</td><td>      current_angle_rad <span class="k3">=</span> d_start_angle_rad <span class="k3">+</span> <span class="k2">(</span>d_q <span class="k3">/</span> d_num_points_minus_one<span class="k2">)</span><span class="k3">*</span>d_angular_length_rad<span class="k2">;</span></td></tr><tr><td class="number">50</td><td>      d_arc_point_x <span class="k3">=</span> d_circ_center_x <span class="k3">+</span> d_radius<span class="k3">*</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>current_angle_rad<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">51</td><td>      d_arc_point_y <span class="k3">=</span> d_circ_center_y <span class="k3">+</span> d_radius<span class="k3">*</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>current_angle_rad<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">52</td><td>      </td></tr><tr><td class="number">53</td><td>      round_double<span class="k2">(</span>d_arc_point_x , arc_point_x<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">54</td><td>      round_double<span class="k2">(</span>d_arc_point_y , arc_point_y<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">55</td><td>      </td></tr><tr><td class="number">56</td><td>      <span class="c">/// use opengl's version of putpixel instead</span></td></tr><tr><td class="number">57</td><td>      <a href="http://www.allegro.cc/manual/putpixel" target="_blank"><span class="a">putpixel</span></a><span class="k2">(</span>dest_bmp , arc_point_x , arc_point_y , color<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">58</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">59</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">60</td><td>  <span class="k1">if</span> <span class="k2">(</span>d_arc_distance_between_points <span class="k3">&gt;</span> <span class="n">1</span>.<span class="n">0</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">61</td><td>    </td></tr><tr><td class="number">62</td><td>    d_angular_length_rad_per_segment <span class="k3">=</span> d_angular_length_rad <span class="k3">/</span> d_num_points_minus_one<span class="k2">;</span></td></tr><tr><td class="number">63</td><td>    <span class="k1">for</span> <span class="k2">(</span>q <span class="k3">=</span> <span class="n">0</span> <span class="k2">;</span> q <span class="k3">&lt;</span> num_segments <span class="k2">;</span> q<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">64</td><td>      d_q <span class="k3">=</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>double&gt;<span class="k2">(</span>q<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">65</td><td>      d_q_plus_one <span class="k3">=</span> <span class="k1">static_cast</span><span class="k3">&lt;</span>double&gt;<span class="k2">(</span>q <span class="k3">+</span> <span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">66</td><td>&#160;</td></tr><tr><td class="number">67</td><td>      current_angle_rad <span class="k3">=</span> d_start_angle_rad <span class="k3">+</span> d_q<span class="k3">*</span>d_angular_length_rad_per_segment<span class="k2">;</span></td></tr><tr><td class="number">68</td><td>      current_angle2_rad <span class="k3">=</span> d_start_angle_rad <span class="k3">+</span> d_q_plus_one<span class="k3">*</span>d_angular_length_rad_per_segment<span class="k2">;</span></td></tr><tr><td class="number">69</td><td>&#160;</td></tr><tr><td class="number">70</td><td>      d_arc_point_x <span class="k3">=</span> d_circ_center_x <span class="k3">+</span> d_radius<span class="k3">*</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>current_angle_rad<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">71</td><td>      d_arc_point_y <span class="k3">=</span> d_circ_center_y <span class="k3">+</span> d_radius<span class="k3">*</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>current_angle_rad<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">72</td><td>&#160;</td></tr><tr><td class="number">73</td><td>      round_double<span class="k2">(</span>d_arc_point_x , arc_point_x<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">74</td><td>      round_double<span class="k2">(</span>d_arc_point_y , arc_point_y<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">75</td><td>&#160;</td></tr><tr><td class="number">76</td><td>      d_arc_point2_x <span class="k3">=</span> d_circ_center_x <span class="k3">+</span> d_radius<span class="k3">*</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>current_angle2_rad<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">77</td><td>      d_arc_point2_y <span class="k3">=</span> d_circ_center_y <span class="k3">+</span> d_radius<span class="k3">*</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>current_angle2_rad<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">78</td><td>&#160;</td></tr><tr><td class="number">79</td><td>      round_double<span class="k2">(</span>d_arc_point2_x , arc_point2_x<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">80</td><td>      round_double<span class="k2">(</span>d_arc_point2_y , arc_point2_y<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">81</td><td>&#160;</td></tr><tr><td class="number">82</td><td>      <span class="c">/// use opengl's version of line instead</span></td></tr><tr><td class="number">83</td><td>      <a href="http://www.allegro.cc/manual/line" target="_blank"><span class="a">line</span></a><span class="k2">(</span>dest_bmp , arc_point_x , arc_point_y , arc_point2_x , arc_point2_y , color<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">84</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">85</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">86</td><td>&#160;</td></tr><tr><td class="number">87</td><td>&#160;</td></tr><tr><td class="number">88</td><td>  <span class="k1">return</span><span class="k2">;</span></td></tr><tr><td class="number">89</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

<s>
I&#39;ve also attached the source code to an example program using this function.
 <a href="http://www.allegro.cc/files/attachment/593865">ArcTest.zip</a>
</s><br />The example program has been revised and will be included in the next post.<br />It has three modes for drawing arcs.<br />In mode 1 you can draw an arc from (a),(b),(c), and (d) above.<br />In mode 2 you can draw an arc between two points , move the points and alter the radius.<br />In mode 3 you can draw an arc from one point through another point to a third point. Each point can be moved with the mouse. This one is the most fun but I like mode 2 also.</p><p>I&#39;ll show how to draw an arc from point A through point B to point C in my next post.</p><p>{Edited formulas for correctness 12-11-2007}
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 05 Dec 2007 14:51:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Edgar Reynaldo rocks, just coming out of nowhere and posting awesomely detailed answers.. unfortunately, I&#39;m not good enough to understand half of it but i really like the time he spends on sharing his knowledge, i think you should make handy tutorials instead of letting things like this fade away as mere replies. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.</p><p>Also, maybe the draw_arc function could make a good contribution too the allegro library? as far as i can tell allegro does not have such a function and i think it might be a cool thing to have included.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Wed, 05 Dec 2007 15:30:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p> <b>{I revised and corrected this post on 12-14-2007}</b></p><p>My previous post in this thread has been updated and corrected. I&#39;ve tested the process for finding the correct information to draw an arc from 2 unique points , a radius , and the direction of the angle drawing the curve. It works now!</p><p>This post has been revised and corrected since the last method was based on incorrect formulas.<br />All mistakes have been fixed and this method works properly now.</p><p>- Albin -<br />Thanks for the appreciation. No problem. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />As far as making tutorials , I don&#39;t mind. I can convert things to wiki pages when I get the time. When I write long replies like this , I always keep it saved as a local text file so I don&#39;t lose my work. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /><br />If you have any questions just ask , I&#39;d be glad to explain.<br />What kind of tutorial would you be interested in? Something on Trigonometry? Geometry?</p><p>BTW , Allegro already has an <a href="http://www.allegro.cc/manual/api/drawing-primitives/arc">arc function</a> that accepts integer circle coordinates and angles in allegro&#39;s fixed data type.</p><p>Other note : The function for drawing an arc that I posted takes degrees and not radians , but could be easily altered to accept radian angles.</p><p> <u>On to cooler things , drawing an arc from point A through point B to point C.</u></p><p>http://www.allegro.cc/files/attachment/593951</p><p>#################### Revision #########################</p><p>Okay , step by step here is how to get the relevant data to draw an arc from point A through point B to point C.</p><p> <u>I. First make sure that all three points are unique (are not equivalent).</u></p><p> <u>II. Find the angles from A to B , from B to C , and from A to C by using the atan2 function :</u></p><p><img class="math" src="http://www.allegro.cc/images/tex/1/e/1ebde48ade1e8f252798b1163aa5ca73-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{AB} = atan2\left((B_y - A_y) , (B_x - A_x)\right)}&lt;/math&gt;" /></p><p><img class="math" src="http://www.allegro.cc/images/tex/3/b/3b9a7e4a22c73e54d9b3c33cb61a3d44-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{BC} = atan2\left((C_y - B_y) , (C_x - B_x)\right)}&lt;/math&gt;" /></p><p><img class="math" src="http://www.allegro.cc/images/tex/3/e/3ec30b8e786b74ea578f75b0d67c3a54-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{AC} = atan2\left((C_y - A_y) , (C_x - A_x)\right)}&lt;/math&gt;" /></p><p>Since the <a href="http://www.delorie.com/djgpp/doc/libc-2.02/libc_46.html">atan2 function</a> returns values in the range from -PI to PI , normalize them to [0,2.0*PI).<br />So if they are less than 0 , then add 2.0*PI.</p><p> <u>III. Determine the direction that the curve of the arc is being drawn and make sure all 3 points are not on a line.</u><br />The next thing we need to do is find angle X which is the change in angle between angle AB and angle AC which determines the direction of the curve of the arc as it is drawn from A through B to C.</p><p><img class="math" src="http://www.allegro.cc/images/tex/8/5/85022615bdacd85f61a7c2121c90af7d-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{AB} + \angle X = \angle\overrightarrow{BC}}&lt;/math&gt;" /></p><p><img class="math" src="http://www.allegro.cc/images/tex/5/d/5d8b175331ee083f134ac7e30805709a-96.png" alt="&lt;math&gt;\mathrm{\angle X = \angle\overrightarrow{BC} - \angle\overrightarrow{AB}}&lt;/math&gt;" /></p><p>In this case let&#39;s normalize the angle to [-PI , PI] so :
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> ang_X <span class="k3">=</span> ang_BtoC <span class="k3">-</span> ang_AtoB<span class="k2">;</span>
<span class="k1">if</span> <span class="k2">(</span>ang_X <span class="k3">&gt;</span> PI<span class="k2">)</span> <span class="k2">{</span>
  ang_X <span class="k3">-</span><span class="k3">=</span> two_pi<span class="k2">;</span>
<span class="k2">}</span> <span class="k1">else</span> <span class="k2">{</span>
  <span class="k1">if</span> <span class="k2">(</span>ang_X <span class="k3">&lt;</span> <span class="k3">-</span><span class="n">1</span>.<span class="n">0</span><span class="k3">*</span>PI<span class="k2">)</span> <span class="k2">{</span>ang_X <span class="k3">+</span><span class="k3">=</span> two_pi<span class="k2">;</span><span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>

Angle X tells us whether the arc from A through B to C is a right handed arc (drawn clockwise) (as the arc is drawn the angle decreases) or if it is a left handed arc (drawn counterclockwise) (as the arc is drawn the angle increases). If angle X is from 0 degrees to 180 degrees (0 to PI) then it is left handed. If angle X is from 180 to 360 degrees (PI to 2*PI) (also -180 to 0 or -PI to 0) then it is right handed. Note that angle X can not be 0 degrees or 180 degrees otherwise all three points are on the same line and could never be points on a circle.</p><p>So now we check whether angle X is equal to 0.0 or whether |angle X| is equal to PI. If either is true then all three points are on the same line and cannot be used to make a circle.</p><p>Now that we know angle X and that the three points are not on a line then from its value we know which direction the curve is being drawn. If angle X is greater than zero then the curve of arc is left handed (counterclockwise) (increasing angles). If angle X is less than zero then the curve of the arc is right handed (clockwise) (decreasing angles).</p><p>if (ang_X &lt; 0.0) {righthanded = true;}<br />if (ang_X &gt; 0.0) {lefthanded = true;}</p><p> <u>IV. Find the midpoints of AB , BC , and AC.</u><br />The midpoint of two points is the sum of their positions divided by two.<br />Point D is the midpoint of AB , point E is the midpoint of AC , and point F is the midpoint of BC.</p><p> <u>V. Find the angles from the center of the circle (point G) to points D , E , and F.</u><br />It is easy to find these angles since they are just one positive quarter turn added to angles AB , AC , and BC.</p><p> <img class="math" src="http://www.allegro.cc/images/tex/c/2/c29d914219cb3163de3204b2ad47be8f-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{GD} = \angle\overrightarrow{AB} + \frac{PI}{2.0}}&lt;/math&gt;" /></p><p> <img class="math" src="http://www.allegro.cc/images/tex/b/f/bfaff4d17b7c79220829f42ea131da58-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{GE} = \angle\overrightarrow{AC} + \frac{PI}{2.0}}&lt;/math&gt;" /></p><p> <img class="math" src="http://www.allegro.cc/images/tex/3/e/3ee4144ed65df61e4be751c7438af6a8-96.png" alt="&lt;math&gt;\mathrm{\angle\overrightarrow{GF} = \angle\overrightarrow{BC} + \frac{PI}{2.0}}&lt;/math&gt;" /></p><p>If any of these angles are greater than or equal to 2.0*PI than subtract 2.0*PI from them to get a normalized angle from [0,2.0*PI).</p><p> <u>VI. Find general first degree equations for the lines through G and D , G and E , and G and F.</u><br />The position where these lines intersect is the center of the circle so first find the general first degree equations for these lines using a point on the line and the angle of the line (D and angle GD , E and angle GE , F and angle GF). So , given a point and an angle , this is how to find the general first degree equation of the line it creates :<br />The form of a general first degree equation is :</p><p><img class="math" src="http://www.allegro.cc/images/tex/c/1/c1fec0c7c2fb426d5d6ddd3ac3eebf9a-96.png" alt="&lt;math&gt;\mathrm{A*x + B*y + C = 0}&lt;/math&gt;" /></p><p>Now take two points <img class="math" src="http://www.allegro.cc/images/tex/4/c/4c7528c9142f0f9f49443ffe58de3630-96.png" alt="&lt;math&gt;\mathrm{(x_1 , y_1) and (x_2 , y_2)}&lt;/math&gt;" /> and assume they are on the same line.<br />Putting them each in the equation separately and subtracting C from both sides of each equation we have :</p><p><img class="math" src="http://www.allegro.cc/images/tex/a/2/a27d9bf80bd24277990ddb21fbfcc623-96.png" alt="&lt;math&gt;\mathrm{A*x_1 + B*y_1 = -C}&lt;/math&gt;" /><br /><img class="math" src="http://www.allegro.cc/images/tex/c/8/c847216ec9c3609e524b32e370b8c3aa-96.png" alt="&lt;math&gt;\mathrm{A*x_2 + B*y_2 = -C}&lt;/math&gt;" /></p><p>Since the left hand side of each equation is equal to negative C then we can set them equal to each other and solve for A or B. This gives us :</p><p><img class="math" src="http://www.allegro.cc/images/tex/8/a/8abf78096597beb6918a8908c775e0e5-96.png" alt="&lt;math&gt;\mathrm{A*x_1 + B*y_1 = A*x_2 + B*y_2}&lt;/math&gt;" /> and then :</p><p><img class="math" src="http://www.allegro.cc/images/tex/5/9/59ce6f0b890a3908aa5a24f5e0d95b30-96.png" alt="&lt;math&gt;\mathrm{A*(x_1 - x_2) = B*(y_2 - y_1)}&lt;/math&gt;" /> so :</p><p><img class="math" src="http://www.allegro.cc/images/tex/f/a/fa044c726bb4f550173cf7bb2bef7bfb-96.png" alt="&lt;math&gt;\mathrm{A = B*\left(\frac{y_2 - y_1}{x_1 - X_2}\right)}&lt;/math&gt;" /></p><p><img class="math" src="http://www.allegro.cc/images/tex/7/6/76af5ee8c5fe80f154a2e89a2837a8f9-96.png" alt="&lt;math&gt;\mathrm{B = A*\left(\frac{x_1 - x_2}{y_2 - y_1}\right)}&lt;/math&gt;" /></p><p>Since we only have one point and an angle to find the equation of the line from , we need to refine these a little. Using trigonometry we can replace (y2 - y1) with r*sin(θ) and replace (x1 - x2) with -r*cos(θ) where θ is the angle of the line. </p><p>This diagram should make it clear.</p><p><span class="remote-thumbnail"><span class="json">{"name":"593952","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/6\/a6e00543828d1cf74db7ab9e8fd637ac.png","w":480,"h":360,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/6\/a6e00543828d1cf74db7ab9e8fd637ac"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/a/6/a6e00543828d1cf74db7ab9e8fd637ac-240.jpg" alt="593952" width="240" height="180" /></span></p><p>Okay , so if we substitute r*sin(θ) and -r*cos(θ) into our equation then the r&#39;s factor out and give us :</p><p>Formula # 1<br /><img class="math" src="http://www.allegro.cc/images/tex/6/3/6325e45b5bd33db8623d7d8fa20a9818-96.png" alt="&lt;math&gt;\mathrm{A = B*\left(\frac{\sin(\theta)}{-1*cos(\theta)}\right)}&lt;/math&gt;" /></p><p>Formula # 2<br /><img class="math" src="http://www.allegro.cc/images/tex/9/9/99914945407ec449916d980df8731637-96.png" alt="&lt;math&gt;\mathrm{B = A*\left(\frac{-1*\cos(\theta)}{sin(\theta)}\right)}&lt;/math&gt;" /></p><p>If the line is either horizontal or vertical then one of these formulas will be undefined due to division by zero , but the other will still be valid. If the line is diagonal then either formula will work but I prefer # 1 because it is easier to translate it to a slope-intercept line equation. Okay , so now we have the ratio between A and B , but how do we go on from here?</p><p>Using N as a constant multiplier of the formula for a first degree general equation we have :<br /><img class="math" src="http://www.allegro.cc/images/tex/1/c/1cf440e2c3fe3ccbc23832c27e923d3d-96.png" alt="&lt;math&gt;\mathrm{N*(A*x + B*y + C) = N*(0)}&lt;/math&gt;" /></p><p>This means that we can scale the equation according to our needs. If we set N = 1/B as long as B != 0 then we have effectively changed B to the value of 1. Similarly this can be used to scale A as well as long as A != 0. This gives us three cases to handle then when we want to find the general first degree equation of a line from a point and an angle :</p><p>Horizontal line (cos(θ) == 0)<br />Formula # 1 becomes invalid so we have to use # 2. Set A equal to 1 , find B , and then solve for C.</p><p>Vertical line (sin(θ) == 0)<br />Formula # 2 becomes invalid so we have to use # 1. Set B equal to 1 , find A , and then solve for C.</p><p>Diagonal line (non-zero values for sin(θ) and cos(θ))<br />Either formula is valid , but formula # 1 is preferable because if you set B to 1 then you can easily find the slope-intercept equation of the line from the general first degree equation by subtracting A*x and C from both sides of the equation.</p><p>Okay , so we use this method to find the general first degree equations of the lines through GD , GE , and GF.</p><p> <u>VII. Now find the intersection of two of the three equations and we have found the center of the circle.</u></p><p>Say we have two first degree general equations like this :</p><p><img class="math" src="http://www.allegro.cc/images/tex/c/1/c1fec0c7c2fb426d5d6ddd3ac3eebf9a-96.png" alt="&lt;math&gt;\mathrm{A*x + B*y + C = 0}&lt;/math&gt;" /><br /><img class="math" src="http://www.allegro.cc/images/tex/c/2/c2be0a427150dcd6ef162217f25ee6a9-96.png" alt="&lt;math&gt;\mathrm{D*x + E*y + F = 0}&lt;/math&gt;" /></p><p>Subtract the constants from each side of the appropriate equation.</p><p><img class="math" src="http://www.allegro.cc/images/tex/a/4/a44d8c1ef3350f73d6408a7b9348c516-96.png" alt="&lt;math&gt;\mathrm{A*x + B*y = -C}&lt;/math&gt;" /><br /><img class="math" src="http://www.allegro.cc/images/tex/1/1/110148bc397ae4c43c3986d80fc3e0fb-96.png" alt="&lt;math&gt;\mathrm{D*x + E*y = -F}&lt;/math&gt;" /></p><p>Now to find x or y then we need to multiply one of the equations by a constant so we can subtract away either x or y.</p><p>To find x , multiply the second equation times (B/E) and then subtract the result from the first equation and solve for x.<br />Sparing you the steps , the result is this :</p><p><img class="math" src="http://www.allegro.cc/images/tex/4/6/46f8f4065d3a96f0fa7dc04bcfd9bbea-96.png" alt="&lt;math&gt;\mathrm{x = \frac{C*E - B*F}{B*D - A*E}}&lt;/math&gt;" /></p><p>To find y , multiply the second equation by (A/D) and then subtract the result from the first equation and solve for y.<br />Which results in :</p><p><img class="math" src="http://www.allegro.cc/images/tex/8/b/8b7d9b056a821f2b7178be55f57c3bad-96.png" alt="&lt;math&gt;\mathrm{y = \frac{A*F - C*D}{B*D - A*E}}&lt;/math&gt;" /></p><p>If B*D is equal to A*E then the lines are parallel and have no strict intersection. They may be the same line or just parallel to each other.</p><p>So now we have equations to find the x and y point of the circle&#39;s center.</p><p> <u>VIII. Find the radius of the circle.</u><br />Now that we have the position of the center of the circle , just use the distance formula to calculate it&#39;s distance from one of the three original points.</p><p> <u>IX. Find the starting angle , ending angle , and then the angular length of the arc.</u></p><p>The starting angle of the arc is the angle from G to A.<br /> <img class="math" src="http://www.allegro.cc/images/tex/c/d/cddf40090d4594149b39402f2deec5c9-96.png" alt="&lt;math&gt;\mathrm{StartingAngle = \angle\overrightarrow{GA} = atan2((A_y - G_y) , (A_x - G_x))}&lt;/math&gt;" /></p><p>The ending angle of the arc is the angle from G to C.<br /> <img class="math" src="http://www.allegro.cc/images/tex/f/7/f7a98c7d7119bf15e786b472f8aa22fa-96.png" alt="&lt;math&gt;\mathrm{EndingAngle = \angle\overrightarrow{GC} = atan2((C_y - G_y) , (C_x - G_x))}&lt;/math&gt;" /></p><p>If we are making a right handed (clockwise , angle decreasing) curving arc then we need to make sure that the ending angle is less than the starting angle so that we get the correct portion of the curve. If the curve is left handed (counterclockwise , angle increasing) then we need to make sure the ending angle is greater than the starting angle.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span>left_handed_arc<span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">if</span> <span class="k2">(</span>EndingAngle <span class="k3">&lt;</span><span class="k3">=</span> StartingAngle<span class="k2">)</span> <span class="k2">{</span>EndingAngle <span class="k3">+</span><span class="k3">=</span> <span class="n">2</span>.<span class="n">0</span><span class="k3">*</span>PI<span class="k2">;</span><span class="k2">}</span>
<span class="k2">}</span>
<span class="k1">if</span> <span class="k2">(</span>right_handed_arc<span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">if</span> <span class="k2">(</span>EndingAngle <span class="k3">&gt;</span><span class="k3">=</span> StartingAngle<span class="k2">)</span> <span class="k2">{</span>EndingAngle <span class="k3">-</span><span class="k3">=</span> <span class="n">2</span>.<span class="n">0</span><span class="k3">*</span>PI<span class="k2">;</span><span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>

So now from the diagram , angle Y is the angular length of the arc to be drawn and  since angle GA plus angle Y equals angle GC then :<br /> <img class="math" src="http://www.allegro.cc/images/tex/2/1/213cfc2c215c4836074726904062e397-96.png" alt="&lt;math&gt;\mathrm{\angle \overrightarrow{GA} + \angle Y = \angle \overrightarrow{GC}}&lt;/math&gt;" /></p><p>So :<br /> <img class="math" src="http://www.allegro.cc/images/tex/1/6/163d3c5dc69e56549f22b6997c48dc01-96.png" alt="&lt;math&gt;\mathrm{AngularLength = \angle Y = \angle \overrightarrow{GC} - \angle \overrightarrow{GA}}&lt;/math&gt;" /></p><p> <u>X. Draw the arc and enjoy!</u></p><p>So finally we have our circle&#39;s center , the radius , the starting angle , and the angular length of the arc so we can pass all of our values to our arc drawing function! <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><p>I have expanded the example program to use three moveable points and display an arc drawn through them.</p><p>Here&#39;s the up to date , working <a href="http://www.allegro.cc/files/attachment/593953">ArcTest source code</a> , just link against Allegro.<br />Here&#39;s the list of keys you can use in the program <a href="http://www.allegro.cc/files/attachment/593959">ArcTest key list</a><br />Here&#39;s a dynamically compiled Windows binary <a href="http://www.allegro.cc/files/attachment/593955">ArcTest binary</a></p><p>Key reference
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>&#160;</td></tr><tr><td class="number">2</td><td>Keys that can be used in any mode</td></tr><tr><td class="number">3</td><td>Numpad <span class="n">1</span>,<span class="n">3</span>     decrease,increase     the distance between points drawn on the <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a></td></tr><tr><td class="number">4</td><td><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a>    R       randomize             the color of the <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a></td></tr><tr><td class="number">5</td><td><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a>    A       clear                 the <a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a></td></tr><tr><td class="number">6</td><td><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a>    I       toggle clearing       on <span class="k1">and</span> off</td></tr><tr><td class="number">7</td><td><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a>    <span class="n">1</span>       <a href="http://www.delorie.com/djgpp/doc/libc/libc_688.html" target="_blank">select</a> mode <span class="n">1</span>         <span class="k1">for</span> drawing an <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a> <span class="k1">using</span> a point , a radius , a starting angle <span class="k1">and</span> angle length</td></tr><tr><td class="number">8</td><td><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a>    <span class="n">2</span>       <a href="http://www.delorie.com/djgpp/doc/libc/libc_688.html" target="_blank">select</a> mode <span class="n">2</span>         <span class="k1">for</span> drawing an <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a> from <span class="n">2</span> points</td></tr><tr><td class="number">9</td><td><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a>    <span class="n">3</span>       <a href="http://www.delorie.com/djgpp/doc/libc/libc_688.html" target="_blank">select</a> mode <span class="n">3</span>         <span class="k1">for</span> drawing an <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a> from point A through point B to point C</td></tr><tr><td class="number">10</td><td><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a>    ESC     quit</td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td>Keys that can be used <span class="k1">while</span> in mode # <span class="n">1</span></td></tr><tr><td class="number">13</td><td>Numpad <span class="n">4</span>,<span class="n">6</span>     decrease,increase     the starting angle of the <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a></td></tr><tr><td class="number">14</td><td>Numpad <span class="n">7</span>,<span class="n">9</span>     decrease,increase     the angular length of the <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a></td></tr><tr><td class="number">15</td><td>Numpad <span class="k3">-</span>,<span class="k3">+</span>     decrease,increase     the radius of the <a href="http://www.allegro.cc/manual/circle" target="_blank"><span class="a">circle</span></a></td></tr><tr><td class="number">16</td><td>Arrow keys     move                  the center of the <a href="http://www.allegro.cc/manual/circle" target="_blank"><span class="a">circle</span></a></td></tr><tr><td class="number">17</td><td>&#160;</td></tr><tr><td class="number">18</td><td>Keys that can be used <span class="k1">while</span> in mode # <span class="n">2</span></td></tr><tr><td class="number">19</td><td>Key     H      reverse the handed-ness of the <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a><span class="s">'s curve (use the other circle'</span>s center point<span class="k2">)</span></td></tr><tr><td class="number">20</td><td>&#160;</td></tr><tr><td class="number">21</td><td>Left mouse click <span class="k1">and</span> drag the <span class="n">2</span> different points that the <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a> is drawn between</td></tr><tr><td class="number">22</td><td>Note <span class="k2">:</span> If it says that the <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a> is invalid , increase the radius with the Numpad <span class="k3">+</span> <a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a></td></tr><tr><td class="number">23</td><td>&#160;</td></tr><tr><td class="number">24</td><td>Keys that can be used <span class="k1">while</span> in mode # <span class="n">3</span></td></tr><tr><td class="number">25</td><td>Left mouse click <span class="k1">and</span> drag the <span class="n">3</span> different points that the <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a> is drawn between</td></tr><tr><td class="number">26</td><td>Note <span class="k2">:</span> If it says that the <a href="http://www.allegro.cc/manual/arc" target="_blank"><span class="a">arc</span></a> is invalid it is because all <span class="n">3</span> points are on the same <a href="http://www.allegro.cc/manual/line" target="_blank"><span class="a">line</span></a> <span class="k1">or</span> two are overlapping <span class="k1">and</span> have the same value</td></tr></tbody></table></div></div><p>

The program is fun , give it a try!</p><p>################# End Revision ############################</p><p>{I revised and corrected this post on 12-14-2007}
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 13 Dec 2007 10:36:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Nice stuff.<br />Testing out your code it seems that it does a complete circle instead of an arc from one point to another, like the allegro arc function.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (juvinious)</author>
		<pubDate>Thu, 13 Dec 2007 22:43:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>- juvinious -<br />No , my arc drawing function just draws an arc of the angular length that you pass to it.</p><p>I have completely revised both of my previous posts in this thread since I screwed up the method used to find an arc from 2 points and a radius or from three points. If anyone was using them before , please reread them.</p><p>Both new methods now work properly and I made a fun example program that uses them. The links to source code , key reference , and windows binary are at the end of my previous post.</p><p>- Edit -<br />Forgot that in mode 2 , the H key reverses which circle is used for the arc (left handed curve or right handed). Key reference and post updated.<br />- End Edit -</p><p>Here are a couple of pics from the program.</p><p>Mode 2<br /><span class="remote-thumbnail"><span class="json">{"name":"593956","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/3\/f3aca1a108e2c1acc922f15a0176f657.jpg","w":640,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/3\/f3aca1a108e2c1acc922f15a0176f657"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/f/3/f3aca1a108e2c1acc922f15a0176f657-240.jpg" alt="593956" width="240" height="180" /></span></p><p>Mode 3<br /><span class="remote-thumbnail"><span class="json">{"name":"593957","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/5\/a53440de8b147ee4fb97dd35ecead201.jpg","w":640,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/5\/a53440de8b147ee4fb97dd35ecead201"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/a/5/a53440de8b147ee4fb97dd35ecead201-240.jpg" alt="593957" width="240" height="180" /></span></p><p>Mode 3 with screen clearing off (key I)<br /><span class="remote-thumbnail"><span class="json">{"name":"593958","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/8\/8848c3c03a3f5fed00471805d9f4adb3.jpg","w":640,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/8\/8848c3c03a3f5fed00471805d9f4adb3"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/8/8/8848c3c03a3f5fed00471805d9f4adb3-240.jpg" alt="593958" width="240" height="180" /></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 14 Dec 2007 15:31:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>My god <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" />  Very nice post!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Fri, 14 Dec 2007 18:56:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Mathematics aside, no one has mentioned <a href="http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/partialdisk.html">gluPartialDisk</a> yet.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Rash)</author>
		<pubDate>Sat, 15 Dec 2007 01:57:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have recently wrote an arc and a circle drawing functions for my game framework. They are unremarkable in terms of their functionality, but I wanted to draw attention to the way I calculate points on the circle. My way is significantly faster than calculating each point using repeated calls of cos and sin. My inspiration came from physics, where the circular motion is described by a constant centripetal acceleration. The algorithm first moves the current point on the tangent line to the circle for some precomputed distance. Then it moves it towards the centre for some precomputed distance. Here they are:</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">void</span> DrawCircle<span class="k2">(</span><span class="k1">float</span> _x, <span class="k1">float</span> _y, <span class="k1">float</span> _r, <span class="k1">float</span> depth, int32_t color, <span class="k1">int</span> segments<span class="k2">)</span></td></tr><tr><td class="number">2</td><td><span class="k2">{</span>    </td></tr><tr><td class="number">3</td><td>    <span class="k1">float</span> r,g,b,a<span class="k2">;</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td>    r <span class="k3">=</span> <span class="k1">float</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/getr32" target="_blank"><span class="a">getr32</span></a><span class="k2">(</span>color<span class="k2">)</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">255</span>.<span class="n">0f</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>    g <span class="k3">=</span> <span class="k1">float</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/getg32" target="_blank"><span class="a">getg32</span></a><span class="k2">(</span>color<span class="k2">)</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">255</span>.<span class="n">0f</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>    b <span class="k3">=</span> <span class="k1">float</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/getb32" target="_blank"><span class="a">getb32</span></a><span class="k2">(</span>color<span class="k2">)</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">255</span>.<span class="n">0f</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>    a <span class="k3">=</span> <span class="k1">float</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/geta32" target="_blank"><span class="a">geta32</span></a><span class="k2">(</span>color<span class="k2">)</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">255</span>.<span class="n">0f</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td>    glColor4f<span class="k2">(</span>r, g, b, a<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>    </td></tr><tr><td class="number">12</td><td>    <span class="k1">float</span> theta <span class="k3">=</span> <span class="n">2</span> <span class="k3">*</span> AL_PI <span class="k3">/</span> <span class="k1">float</span><span class="k2">(</span>segments<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>    <span class="k1">float</span> tangetial_factor <span class="k3">=</span> tanf<span class="k2">(</span>theta<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>    <span class="k1">float</span> radial_factor <span class="k3">=</span> <span class="n">1</span> <span class="k3">-</span> cosf<span class="k2">(</span>theta<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>    </td></tr><tr><td class="number">16</td><td>    <span class="k1">float</span> x <span class="k3">=</span> _x <span class="k3">+</span> _r<span class="k2">;</span></td></tr><tr><td class="number">17</td><td>    <span class="k1">float</span> y <span class="k3">=</span> _y<span class="k2">;</span></td></tr><tr><td class="number">18</td><td>    </td></tr><tr><td class="number">19</td><td>    glBegin<span class="k2">(</span>GL_LINE_LOOP<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">20</td><td>    <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> ii <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> ii <span class="k3">&lt;</span> segments<span class="k2">;</span> ii<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></td></tr><tr><td class="number">21</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">22</td><td>        glVertex3f<span class="k2">(</span>x, y, depth<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>        </td></tr><tr><td class="number">24</td><td>        <span class="k1">float</span> tx <span class="k3">=</span> <span class="k3">-</span><span class="k2">(</span>y <span class="k3">-</span> _y<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">25</td><td>        <span class="k1">float</span> ty <span class="k3">=</span> x <span class="k3">-</span> _x<span class="k2">;</span></td></tr><tr><td class="number">26</td><td>        </td></tr><tr><td class="number">27</td><td>        x <span class="k3">+</span><span class="k3">=</span> tx <span class="k3">*</span> tangetial_factor<span class="k2">;</span></td></tr><tr><td class="number">28</td><td>        y <span class="k3">+</span><span class="k3">=</span> ty <span class="k3">*</span> tangetial_factor<span class="k2">;</span></td></tr><tr><td class="number">29</td><td>        </td></tr><tr><td class="number">30</td><td>        <span class="k1">float</span> rx <span class="k3">=</span> _x <span class="k3">-</span> x<span class="k2">;</span></td></tr><tr><td class="number">31</td><td>        <span class="k1">float</span> ry <span class="k3">=</span> _y <span class="k3">-</span> y<span class="k2">;</span></td></tr><tr><td class="number">32</td><td>        </td></tr><tr><td class="number">33</td><td>        x <span class="k3">+</span><span class="k3">=</span> rx <span class="k3">*</span> radial_factor<span class="k2">;</span></td></tr><tr><td class="number">34</td><td>        y <span class="k3">+</span><span class="k3">=</span> ry <span class="k3">*</span> radial_factor<span class="k2">;</span></td></tr><tr><td class="number">35</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">36</td><td>    glEnd<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">37</td><td>    </td></tr><tr><td class="number">38</td><td>    glColor4f<span class="k2">(</span><span class="n">1</span>.<span class="n">0f</span>, <span class="n">1</span>.<span class="n">0f</span>, <span class="n">1</span>.<span class="n">0f</span>, <span class="n">1</span>.<span class="n">0f</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">39</td><td><span class="k2">}</span></td></tr><tr><td class="number">40</td><td>&#160;</td></tr><tr><td class="number">41</td><td><span class="c">//segments - number of segments per full revolution!</span></td></tr><tr><td class="number">42</td><td><span class="c">//d_angle - determines the direction and the ammount of the arc to draw</span></td></tr><tr><td class="number">43</td><td><span class="k1">void</span> DrawArc<span class="k2">(</span><span class="k1">float</span> _x, <span class="k1">float</span> _y, <span class="k1">float</span> _r, <span class="k1">float</span> depth, <span class="k1">float</span> start_angle, <span class="k1">float</span> d_angle, int32_t color, <span class="k1">int</span> segments<span class="k2">)</span></td></tr><tr><td class="number">44</td><td><span class="k2">{</span></td></tr><tr><td class="number">45</td><td>    <span class="k1">float</span> r,g,b,a<span class="k2">;</span></td></tr><tr><td class="number">46</td><td>&#160;</td></tr><tr><td class="number">47</td><td>    r <span class="k3">=</span> <span class="k1">float</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/getr32" target="_blank"><span class="a">getr32</span></a><span class="k2">(</span>color<span class="k2">)</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">255</span>.<span class="n">0f</span><span class="k2">;</span></td></tr><tr><td class="number">48</td><td>    g <span class="k3">=</span> <span class="k1">float</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/getg32" target="_blank"><span class="a">getg32</span></a><span class="k2">(</span>color<span class="k2">)</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">255</span>.<span class="n">0f</span><span class="k2">;</span></td></tr><tr><td class="number">49</td><td>    b <span class="k3">=</span> <span class="k1">float</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/getb32" target="_blank"><span class="a">getb32</span></a><span class="k2">(</span>color<span class="k2">)</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">255</span>.<span class="n">0f</span><span class="k2">;</span></td></tr><tr><td class="number">50</td><td>    a <span class="k3">=</span> <span class="k1">float</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/geta32" target="_blank"><span class="a">geta32</span></a><span class="k2">(</span>color<span class="k2">)</span><span class="k2">)</span> <span class="k3">/</span> <span class="n">255</span>.<span class="n">0f</span><span class="k2">;</span></td></tr><tr><td class="number">51</td><td>&#160;</td></tr><tr><td class="number">52</td><td>    glColor4f<span class="k2">(</span>r, g, b, a<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">53</td><td>    </td></tr><tr><td class="number">54</td><td>    <span class="k1">int</span> real_segments <span class="k3">=</span> <span class="k1">int</span><span class="k2">(</span>fabsf<span class="k2">(</span>d_angle<span class="k2">)</span> <span class="k3">/</span> <span class="k2">(</span><span class="n">2</span> <span class="k3">*</span> AL_PI<span class="k2">)</span> <span class="k3">*</span> <span class="k2">(</span><span class="k1">float</span><span class="k2">)</span>segments<span class="k2">)</span> <span class="k3">+</span> <span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">55</td><td>    </td></tr><tr><td class="number">56</td><td>    <span class="k1">float</span> theta <span class="k3">=</span> d_angle <span class="k3">/</span> <span class="k1">float</span><span class="k2">(</span>real_segments<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">57</td><td>    <span class="k1">float</span> tangetial_factor <span class="k3">=</span> tanf<span class="k2">(</span>theta<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">58</td><td>    <span class="k1">float</span> radial_factor <span class="k3">=</span> <span class="n">1</span> <span class="k3">-</span> cosf<span class="k2">(</span>theta<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">59</td><td>    </td></tr><tr><td class="number">60</td><td>    <span class="k1">float</span> x <span class="k3">=</span> _x <span class="k3">+</span> _r <span class="k3">*</span> cosf<span class="k2">(</span>start_angle<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">61</td><td>    <span class="k1">float</span> y <span class="k3">=</span> _y <span class="k3">+</span> _r <span class="k3">*</span> sinf<span class="k2">(</span>start_angle<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">62</td><td>    </td></tr><tr><td class="number">63</td><td>    glBegin<span class="k2">(</span>GL_LINE_STRIP<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">64</td><td>    <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> ii <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> ii <span class="k3">&lt;</span> real_segments <span class="k3">+</span> <span class="n">1</span><span class="k2">;</span> ii<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></td></tr><tr><td class="number">65</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">66</td><td>        glVertex3f<span class="k2">(</span>x, y, depth<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">67</td><td>        </td></tr><tr><td class="number">68</td><td>        <span class="k1">float</span> tx <span class="k3">=</span> <span class="k3">-</span><span class="k2">(</span>y <span class="k3">-</span> _y<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">69</td><td>        <span class="k1">float</span> ty <span class="k3">=</span> x <span class="k3">-</span> _x<span class="k2">;</span></td></tr><tr><td class="number">70</td><td>        </td></tr><tr><td class="number">71</td><td>        x <span class="k3">+</span><span class="k3">=</span> tx <span class="k3">*</span> tangetial_factor<span class="k2">;</span></td></tr><tr><td class="number">72</td><td>        y <span class="k3">+</span><span class="k3">=</span> ty <span class="k3">*</span> tangetial_factor<span class="k2">;</span></td></tr><tr><td class="number">73</td><td>        </td></tr><tr><td class="number">74</td><td>        <span class="k1">float</span> rx <span class="k3">=</span> _x <span class="k3">-</span> x<span class="k2">;</span></td></tr><tr><td class="number">75</td><td>        <span class="k1">float</span> ry <span class="k3">=</span> _y <span class="k3">-</span> y<span class="k2">;</span></td></tr><tr><td class="number">76</td><td>        </td></tr><tr><td class="number">77</td><td>        x <span class="k3">+</span><span class="k3">=</span> rx <span class="k3">*</span> radial_factor<span class="k2">;</span></td></tr><tr><td class="number">78</td><td>        y <span class="k3">+</span><span class="k3">=</span> ry <span class="k3">*</span> radial_factor<span class="k2">;</span></td></tr><tr><td class="number">79</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">80</td><td>    glEnd<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">81</td><td>    </td></tr><tr><td class="number">82</td><td>    glColor4f<span class="k2">(</span><span class="n">1</span>.<span class="n">0f</span>, <span class="n">1</span>.<span class="n">0f</span>, <span class="n">1</span>.<span class="n">0f</span>, <span class="n">1</span>.<span class="n">0f</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">83</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Sat, 15 Dec 2007 06:44:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>- SiegeLord -<br />Your approach looks intriguing , although I have a few questions.</p><p>Are your sinf , cosf , and tanf functions just wrappers for the sin,cos,and tan C functions that cast the float to a double , apply the function and then cast it back to a float and return the value?</p><p>I don&#39;t quite understand the geometry behind how you adjust the position to plot each time through the loop. The radial_factor , 1 - cosf(theta) , what does that represent geometrically?</p><p>Don&#39;t you get compiler redeclaration errors in the for loops when you declare float tx,ty , rx,ry in each iteration?</p><p>As a note , when you assign &quot;float tx = -(y - _y);&quot; , you could just rewrite that as &quot;tx = _y - y;&quot; and save a negation. Not that it matters codewise , but tangetial_factor is misspelled , a missing &quot;n&quot;.</p><p>Anyways , it&#39;s a cool idea , thanks for sharing. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 15 Dec 2007 11:47:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I can respond to some of the questions.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Are your sinf , cosf , and tanf functions just wrappers...
</p></div></div><p>
sinf, cosf and tanf are standard C99 functions, nothing special, they just take and return a float.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Don&#39;t you get compiler redeclaration errors in the for loops when you declare float tx,ty , rx,ry in each iteration?
</p></div></div><p>
No. You do? The variable goes out of scope on each loop iteration.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Sat, 15 Dec 2007 16:40:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Don&#39;t you get compiler redeclaration errors in the for loops when you declare float tx,ty , rx,ry in each iteration?
</p></div></div><p>
No. You do? The variable goes out of scope on each loop iteration.
</p></div></div><p>

Tried a simple test and it works just fine. Silly me.<br />Why not just declare them once outside the for loop though?</p><p>Can anyone explain what the radial_factor (1 - cosf(theta)) represents geometrically?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sun, 16 Dec 2007 09:08:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the typo corrections... this code shall be part of my ChristMassHack entry, and I&#39;d rather it not to be too incomprehensible... <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Why not just declare them once outside the for loop though?
</p></div></div><p>
I believe that that is just a style preference.</p><p>Anyway, here is a geometric explanation of my algorithm.<br /> <br />http://www.allegro.cc/files/attachment/593978<br /> <br />In this diagram we start on point <i>A</i>. We know the location of point <i>O</i> (it is one of our function parameters) and we know the length <i>OA</i>. We also know theta, since it simply equals the angular size of the circle or arc we wish to draw divided by the number of segments.</p><p>So, as per our algorithm we first move tangentially to point <i>K</i> and then move radially to point <i>B</i> - which is the second point on our circle and would be connected to <i>A</i> by some sort of a line, OpenGL or otherwise. Now, since we know <i>OA</i> and we know theta we can calculate the magnitude of <i>KA</i> quite easily. <br />It is:</p><p> <img class="math" src="http://www.allegro.cc/images/tex/6/e/6e0ad2d20544991f423d192dc0886e8a-96.png" alt="&lt;math&gt;\frac{KA}{OA} = tan(\theta)&lt;/math&gt;" /></p><p>or:</p><p><img class="math" src="http://www.allegro.cc/images/tex/f/e/fe1d1a9211497923e05a2640bf8069d2-96.png" alt="&lt;math&gt;KA = OA tan(\theta)&lt;/math&gt;" /></p><p>So our tangential factor is known. And amusing property of this factor is that it is negative when theta is negative, which allows for negative angular lengths for arcs. Now, what is the direction of <i>KA</i>? It is simply the normal vector to <i>OA</i>. We just exchange the components of the radius and negativize one of them. This is where my awkward &quot;float tx = -(y - _y);&quot; comes from. I just wanted to emphasize the fact that that is a normal(tangent) vector.</p><p>Thus, to find <i>KA</i> all we do is find the normal of <i>OA</i> and multiply it by the tangential factor.</p><p>Now, how to find <i>KB</i>, our radial vector that we move on to get to <i>B</i>? It is important to realize that we know what <i>KO</i> is, since we know where <i>K</i> is from the previous compuation (just add the <i>KA</i> vector to the point <i>A</i>). We also know <i>OA</i> and thus <i>OB</i>. Now, we form two equations:</p><p>1) <img class="math" src="http://www.allegro.cc/images/tex/2/f/2f9a24c61933b3f8cc20a6f06f6c5236-96.png" alt="&lt;math&gt;\frac{OA}{KO} = cos(\theta)&lt;/math&gt;" /></p><p>which reduces to:</p><p>1) <img class="math" src="http://www.allegro.cc/images/tex/a/3/a3875e534517738b06a0172460be857e-96.png" alt="&lt;math&gt;OA = KO cos(\theta)&lt;/math&gt;" /></p><p>And equation 2:</p><p>2) <img class="math" src="http://www.allegro.cc/images/tex/a/b/abfad460d098bb6da8cc07e3179d1b05-96.png" alt="&lt;math&gt;OB + KB = KO&lt;/math&gt;" /></p><p>Since <i>OA</i> = <i>OB</i>, we can plug equation 1 into 2 to get:</p><p><img class="math" src="http://www.allegro.cc/images/tex/3/a/3a2d4e1d08c1d0f30832ef12a9646b84-96.png" alt="&lt;math&gt;KO cos(\theta) + KB = KO&lt;/math&gt;" /></p><p>Simplifying that we get:</p><p><img class="math" src="http://www.allegro.cc/images/tex/8/f/8f3c4f006846c8696398f50ae7dfcf63-96.png" alt="&lt;math&gt;KB = KO (1 - cos(\theta))&lt;/math&gt;" /></p><p>So, our radial factor is 1 - cos(theta).</p><p>Thus, to get <i>KB</i> we would simply find the vector <i>KO</i> from <i>K</i> and <i>O</i> and then multiply it by our factor.</p><p>If we add that vector to <i>K</i> we will get the point <i>B</i>. Repeat that however number of times and you will get a number of equally spaced points on the circle. As you can see we only needed to precalculate the two factors. Everything else is determined iteratively in the main loop itself.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Sun, 16 Dec 2007 12:37:47 +0000</pubDate>
	</item>
</rss>
