mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
245 lines
14 KiB
HTML
245 lines
14 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
||
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
||
|
<meta name="generator" content="Doxygen 1.8.18"/>
|
||
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||
|
<title>Algorithms_in_C: graphics/spirograph.c File Reference</title>
|
||
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
||
|
<script type="text/javascript" src="../../jquery.js"></script>
|
||
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
||
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
||
|
<script type="text/javascript" src="../../resize.js"></script>
|
||
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
||
|
<script type="text/javascript" src="../../navtree.js"></script>
|
||
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
||
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
||
|
<script type="text/javascript" src="../../search/search.js"></script>
|
||
|
<script type="text/x-mathjax-config">
|
||
|
MathJax.Hub.Config({
|
||
|
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
||
|
jax: ["input/TeX","output/HTML-CSS"],
|
||
|
});
|
||
|
</script>
|
||
|
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
||
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
||
|
</head>
|
||
|
<body>
|
||
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||
|
<div id="titlearea">
|
||
|
<table cellspacing="0" cellpadding="0">
|
||
|
<tbody>
|
||
|
<tr style="height: 56px;">
|
||
|
<td id="projectalign" style="padding-left: 0.5em;">
|
||
|
<div id="projectname">Algorithms_in_C
|
||
|
 <span id="projectnumber">1.0.0</span>
|
||
|
</div>
|
||
|
<div id="projectbrief">Set of algorithms implemented in C.</div>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</table>
|
||
|
</div>
|
||
|
<!-- end header part -->
|
||
|
<!-- Generated by Doxygen 1.8.18 -->
|
||
|
<script type="text/javascript">
|
||
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
||
|
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
|
||
|
/* @license-end */
|
||
|
</script>
|
||
|
<script type="text/javascript" src="../../menudata.js"></script>
|
||
|
<script type="text/javascript" src="../../menu.js"></script>
|
||
|
<script type="text/javascript">
|
||
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
||
|
$(function() {
|
||
|
initMenu('../../',true,false,'search.php','Search');
|
||
|
$(document).ready(function() { init_search(); });
|
||
|
});
|
||
|
/* @license-end */</script>
|
||
|
<div id="main-nav"></div>
|
||
|
</div><!-- top -->
|
||
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||
|
<div id="nav-tree">
|
||
|
<div id="nav-tree-contents">
|
||
|
<div id="nav-sync" class="sync"></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="splitbar" style="-moz-user-select:none;"
|
||
|
class="ui-resizable-handle">
|
||
|
</div>
|
||
|
</div>
|
||
|
<script type="text/javascript">
|
||
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
||
|
$(document).ready(function(){initNavTree('d7/d98/spirograph_8c.html','../../'); initResizable(); });
|
||
|
/* @license-end */
|
||
|
</script>
|
||
|
<div id="doc-content">
|
||
|
<!-- window showing the filter options -->
|
||
|
<div id="MSearchSelectWindow"
|
||
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
||
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
||
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
||
|
</div>
|
||
|
|
||
|
<!-- iframe showing the search results (closed by default) -->
|
||
|
<div id="MSearchResultsWindow">
|
||
|
<iframe src="javascript:void(0)" frameborder="0"
|
||
|
name="MSearchResults" id="MSearchResults">
|
||
|
</iframe>
|
||
|
</div>
|
||
|
|
||
|
<div class="header">
|
||
|
<div class="summary">
|
||
|
<a href="#define-members">Macros</a> |
|
||
|
<a href="#func-members">Functions</a> </div>
|
||
|
<div class="headertitle">
|
||
|
<div class="title">spirograph.c File Reference</div> </div>
|
||
|
</div><!--header-->
|
||
|
<div class="contents">
|
||
|
|
||
|
<p>Implementation of <a href="https://en.wikipedia.org/wiki/Spirograph">Spirograph</a>
|
||
|
<a href="#details">More...</a></p>
|
||
|
<div class="textblock"><code>#include <math.h></code><br />
|
||
|
<code>#include <stdbool.h></code><br />
|
||
|
<code>#include <stdio.h></code><br />
|
||
|
<code>#include <stdlib.h></code><br />
|
||
|
<code>#include <time.h></code><br />
|
||
|
</div><div class="textblock"><div class="dynheader">
|
||
|
Include dependency graph for spirograph.c:</div>
|
||
|
<div class="dyncontent">
|
||
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/dd5/spirograph_8c__incl.svg" width="430" height="112"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div><table class="memberdecls">
|
||
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="define-members"></a>
|
||
|
Macros</h2></td></tr>
|
||
|
<tr class="memitem:a525335710b53cb064ca56b936120431e"><td class="memItemLeft" align="right" valign="top"><a id="a525335710b53cb064ca56b936120431e"></a>
|
||
|
#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d98/spirograph_8c.html#a525335710b53cb064ca56b936120431e">_USE_MATH_DEFINES</a></td></tr>
|
||
|
<tr class="memdesc:a525335710b53cb064ca56b936120431e"><td class="mdescLeft"> </td><td class="mdescRight">required for MSVC compiler <br /></td></tr>
|
||
|
<tr class="separator:a525335710b53cb064ca56b936120431e"><td class="memSeparator" colspan="2"> </td></tr>
|
||
|
</table><table class="memberdecls">
|
||
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
|
||
|
Functions</h2></td></tr>
|
||
|
<tr class="memitem:a0daa148091ec953809fc172289f773d3"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d98/spirograph_8c.html#a0daa148091ec953809fc172289f773d3">spirograph</a> (double *x, double *y, double l, double k, size_t <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, double rot)</td></tr>
|
||
|
<tr class="memdesc:a0daa148091ec953809fc172289f773d3"><td class="mdescLeft"> </td><td class="mdescRight">Generate spirograph curve into arrays <code>x</code> and <code>y</code> such that the i^th point in 2D is represented by <code>(x[i],y[i])</code>. <a href="../../d7/d98/spirograph_8c.html#a0daa148091ec953809fc172289f773d3">More...</a><br /></td></tr>
|
||
|
<tr class="separator:a0daa148091ec953809fc172289f773d3"><td class="memSeparator" colspan="2"> </td></tr>
|
||
|
<tr class="memitem:a708a4c1a4d0c4acc4c447310dd4db27f"><td class="memItemLeft" align="right" valign="top"><a id="a708a4c1a4d0c4acc4c447310dd4db27f"></a>
|
||
|
void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d98/spirograph_8c.html#a708a4c1a4d0c4acc4c447310dd4db27f">test</a> (void)</td></tr>
|
||
|
<tr class="memdesc:a708a4c1a4d0c4acc4c447310dd4db27f"><td class="mdescLeft"> </td><td class="mdescRight">Test function to save resulting points to a CSV file. <br /></td></tr>
|
||
|
<tr class="separator:a708a4c1a4d0c4acc4c447310dd4db27f"><td class="memSeparator" colspan="2"> </td></tr>
|
||
|
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top"><a id="a3c04138a5bfe5d72780bb7e82a18e627"></a>
|
||
|
int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/d98/spirograph_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
|
||
|
<tr class="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <br /></td></tr>
|
||
|
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2"> </td></tr>
|
||
|
</table>
|
||
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
||
|
<div class="textblock"><p>Implementation of <a href="https://en.wikipedia.org/wiki/Spirograph">Spirograph</a> </p>
|
||
|
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala">Krishna Vedala</a></dd></dl>
|
||
|
<p>Implementation of the program is based on the geometry shown in the figure below:</p>
|
||
|
<p><a href="https://commons.wikimedia.org/wiki/File:Resonance_Cascade.svg"><img src="https://upload.wikimedia.org/wikipedia/commons/3/39/Resonance_Cascade.svg" alt="Spirograph geometry from Wikipedia" style="pointer-events: none; width: 250px" class="inline"/></a> </p>
|
||
|
</div><h2 class="groupheader">Function Documentation</h2>
|
||
|
<a id="a0daa148091ec953809fc172289f773d3"></a>
|
||
|
<h2 class="memtitle"><span class="permalink"><a href="#a0daa148091ec953809fc172289f773d3">◆ </a></span>spirograph()</h2>
|
||
|
|
||
|
<div class="memitem">
|
||
|
<div class="memproto">
|
||
|
<table class="memname">
|
||
|
<tr>
|
||
|
<td class="memname">void spirograph </td>
|
||
|
<td>(</td>
|
||
|
<td class="paramtype">double * </td>
|
||
|
<td class="paramname"><em>x</em>, </td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td class="paramkey"></td>
|
||
|
<td></td>
|
||
|
<td class="paramtype">double * </td>
|
||
|
<td class="paramname"><em>y</em>, </td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td class="paramkey"></td>
|
||
|
<td></td>
|
||
|
<td class="paramtype">double </td>
|
||
|
<td class="paramname"><em>l</em>, </td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td class="paramkey"></td>
|
||
|
<td></td>
|
||
|
<td class="paramtype">double </td>
|
||
|
<td class="paramname"><em>k</em>, </td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td class="paramkey"></td>
|
||
|
<td></td>
|
||
|
<td class="paramtype">size_t </td>
|
||
|
<td class="paramname"><em>N</em>, </td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td class="paramkey"></td>
|
||
|
<td></td>
|
||
|
<td class="paramtype">double </td>
|
||
|
<td class="paramname"><em>rot</em> </td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td></td>
|
||
|
<td>)</td>
|
||
|
<td></td><td></td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div><div class="memdoc">
|
||
|
|
||
|
<p>Generate spirograph curve into arrays <code>x</code> and <code>y</code> such that the i^th point in 2D is represented by <code>(x[i],y[i])</code>. </p>
|
||
|
<p>The generating function is given by: </p><p class="formulaDsp">
|
||
|
\begin{eqnarray*} x &=& R\left[ (1-k) \cos (t) + l\cdot k\cdot\cos \left(\frac{1-k}{k}t\right) \right]\\ y &=& R\left[ (1-k) \sin (t) - l\cdot k\cdot\sin \left(\frac{1-k}{k}t\right) \right] \end{eqnarray*}
|
||
|
</p>
|
||
|
<p> where</p><ul>
|
||
|
<li>\(R\) is the scaling parameter that we will consider \(=1\)</li>
|
||
|
<li>\(l=\frac{\rho}{r}\) is the relative distance of marker from the centre of inner circle and \(0\le l\le1\)</li>
|
||
|
<li>\(\rho\) is physical distance of marker from centre of inner circle</li>
|
||
|
<li>\(r\) is the radius of inner circle</li>
|
||
|
<li>\(k=\frac{r}{R}\) is the ratio of radius of inner circle to outer circle and \(0<k<1\)</li>
|
||
|
<li>\(R\) is the radius of outer circle</li>
|
||
|
<li>\(t\) is the angle of rotation of the point i.e., represents the time parameter</li>
|
||
|
</ul>
|
||
|
<p>Since we are considering ratios, the actual values of \(r\) and \(R\) are immaterial.</p>
|
||
|
<dl class="params"><dt>Parameters</dt><dd>
|
||
|
<table class="params">
|
||
|
<tr><td class="paramdir">[out]</td><td class="paramname">x</td><td>output array containing absicca of points (must be pre-allocated) </td></tr>
|
||
|
<tr><td class="paramdir">[out]</td><td class="paramname">y</td><td>output array containing ordinates of points (must be pre-allocated) </td></tr>
|
||
|
<tr><td class="paramdir"></td><td class="paramname">l</td><td>the relative distance of marker from the centre of inner circle and \(0\le l\le1\) </td></tr>
|
||
|
<tr><td class="paramdir"></td><td class="paramname">k</td><td>the ratio of radius of inner circle to outer circle and \(0<k<1\) </td></tr>
|
||
|
<tr><td class="paramdir"></td><td class="paramname">N</td><td>number of sample points along the trajectory (higher = better resolution but consumes more time and memory) </td></tr>
|
||
|
<tr><td class="paramdir"></td><td class="paramname">num_rot</td><td>the number of rotations to perform (can be fractional value) </td></tr>
|
||
|
</table>
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<div class="fragment"><div class="line"><a name="l00058"></a><span class="lineno"> 58</span> {</div>
|
||
|
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>  <span class="keywordtype">double</span> dt = rot * 2.f * M_PI / <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>;</div>
|
||
|
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>  <span class="keywordtype">double</span> t = 0.f, R = 1.f;</div>
|
||
|
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>  <span class="keyword">const</span> <span class="keywordtype">double</span> k1 = 1.f - k;</div>
|
||
|
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  </div>
|
||
|
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>  <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> dk = 0; dk < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; dk++, t += dt)</div>
|
||
|
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>  {</div>
|
||
|
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>  x[dk] = R * (k1 * cos(t) + l * k * cos(k1 * t / k));</div>
|
||
|
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>  y[dk] = R * (k1 * sin(t) - l * k * sin(k1 * t / k));</div>
|
||
|
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>  }</div>
|
||
|
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span> }</div>
|
||
|
</div><!-- fragment -->
|
||
|
</div>
|
||
|
</div>
|
||
|
</div><!-- contents -->
|
||
|
</div><!-- doc-content -->
|
||
|
<div class="ttc" id="aproblem__13_2sol1_8c_html_a0240ac851181b84ac374872dc5434ee4"><div class="ttname"><a href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a></div><div class="ttdeci">#define N</div><div class="ttdoc">number of digits of the large number</div><div class="ttdef"><b>Definition:</b> sol1.c:109</div></div>
|
||
|
<!-- start footer part -->
|
||
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
||
|
<ul>
|
||
|
<li class="navelem"><a class="el" href="../../dir_e79632891301b850df87e9c0030293fa.html">graphics</a></li><li class="navelem"><a class="el" href="../../d7/d98/spirograph_8c.html">spirograph.c</a></li>
|
||
|
<li class="footer">Generated by
|
||
|
<a href="http://www.doxygen.org/index.html">
|
||
|
<img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|