# What is the tangent point of any given co-prime on the Mandelbrot Set in pseudocode?

Given a computer program generating the Mandelbrot Set - using this one for example, which uses a module called mandel.js - what would be the pseudocode to find the complex coordinates, capable of creating a highlighted point representing the tangent where that co-prime's bulb connects, from two entered p and q values?

Refer to the passage here and the accompanying equation beginning C p/q = ...

So far I have tried:

part1 = (E^(2.0 * PI * i * (p div q))) div 2.0
part2 = 1.0 - (E^(2.0 * PI * i * (p div q))) div 2.0


Where p and q are co-primes, PI and E are the expected constants, and i is set to 1 or -1 (not sure of the correct handling of i in this context).

This simply tries to solve two parts of the equation given p and q, and does not seem to work. I am having trouble (1) programmatically translating the fundamental equation to code, and (2) conceptually relating the resulting parameter solution set for co-primes given p and q in the equation referenced, which I don't fully understand, to the complex plane as the standard screen geometry x,y pixel-painting used in the program, which I can understand.

SOLUTION:

Thanks to Jim's helpfulness, here is the pseudocode I came up with and tested in a working fork of the M-set program mentioned:

1 epower = ((p * 2 * PI)/q)
2 expr = CPLX(COS(epower),SIN(epower))
3 divBy = CPLX(2,0)
4 subFrom = CPLX(1,0)
5 term1 = CPLX.divide(expr,divBy)
6 term2 = CPLX.subtract(subFrom,term1)
7 result = CPLX.multiply(term1,term2)
8 x = result.real
9 y = result.imag


Line 1 is the complex power of e in the equation. Line 2 required a bit of research - it is apparently an application of de Moivre's formula. I imported and stitched together a custom complex number object from two examples I found on the web. This library is indicated as CPLX. It's constructor takes two parts, a real and imaginary, to create a complex variable. It then supports the basic operations. Lines 3 and 4 are just real numbers turned into complex number form to support the operations in lines 5, 6, 7. Lines 8 and 9 return the resulting complex number parts, which can be translated into the screen geometry of the M-set display.

-
Please do not post in the imperative. This seems to me a huge assignment to give free help. What have you done? Where are you stuck? – Ross Millikan Jun 8 '11 at 20:20
@Ross - I have tried to fix the question. Yes it is significant effort. If it helps, I plan to cross-post a good answer to the referenced Wikipedia article - there is very good pseudocode to generate M-sets given there already, and I believe both equations and pseudocode are essential to the many interest and skill levels that might be approaching this. – KTys Jun 8 '11 at 21:02
From what I understand, you want the program to take $p$ and $q$ as input, and then draw a point at $c_{p/q}$, where $c_{p/q}$ is defined by the formula in the Wikipedia article. What I don't understand is your description of what you're having trouble with. Shouldn't this just be a matter of plugging the formula into the program and then drawing the point? – Jim Belk Jun 8 '11 at 21:52
@Jim - I tried plugging and solving. The expected range for the M-set is -2 to 1 on the real and -1 to 1 on the imaginary axes. I could not get values in that range - so presumably I did not set up or perform your recommended calculation correctly. An example, based on the referenced equation or other method, of finding the real and imaginary coordinates for the p/q ratios 1/4, 2/5, 1/3 (these can be readily checked visually) would be a good answer to the question. – KTys Jun 8 '11 at 23:51

Here's an example of how to compute a $c_{p/q}$ value in the case where $p/q=1/3$. The formula given by Wikipedia is $$c_{p/q} \;=\; \frac{e^{2\pi i p/q}}{2}\left(1-\frac{e^{2\pi i p/q}}{2}\right)\text{,}$$ so \begin{align*} c_{1/3} \;&=\; \frac{e^{2\pi i/3}}{2}\left(1-\frac{e^{2\pi i/3}}{2}\right) \\ \\ &=\; \frac{-0.5 + 0.866025 i}{2}\left(1-\frac{-0.5 + 0.866025 i}{2}\right) \\ \\ &=\; (-0.25 + 0.433013 i)(1.25-0.433013 i) \\ \\ &=\; -0.125 + 0.64952 i. \end{align*}
Edit: In general, the formula for $c_{p/q}$ using real numbers is $$c_{p/q} \;=\; \frac{2 \cos (2 \pi p/q)-\cos (4 \pi p/q)}{4} \;+\; 2 \sin ^3(\pi p/q) \cos (\pi p/q)\; i$$