1

As an expansion to my fractal software, I've decided to add center plus width as an image location/definition. Which leads me to the question of how do I convert the complex number given to an x,y coordinate on the screen plane. Obviously, I do this when I calculate the image as I iterate a given function, but I do that in terms that are not applicable to the 'free-standing' number I want to deal with. I can go in the other direction with no problem but have not been able to figure this one out. The downside of doing mathematics without being a mathematician! In short, complex to screen conversion—how? As an example of screen to complex conversion her is the function I use for that:

scr2Pix = (pX, pY, pW, pH, minX, maxX, minY, maxY) ->
  xPercent = pX / pW
  yPercent = pY / pH
  newX = minX + (maxX - minX) * xPercent;
  newY = minY + (maxY - minY) * yPercent;
  [newX, newY]

The language is coffeescript. The parameters are better shown in this snippet:

x1 = -2.5 #! lower left X—complex
y1 = -1.5 #! lower left Y—complex
x2 = 1.5  #! upper right X—complex
y2 = 1.5  #! upper right Y—complex

console.log scr2Pix(320,240,640,480,x1,x2,y1,y2)

with a return value of [-0.5, 0], in effect asking for the conversion of 320,240 to a complex number. NB pW is screen width, pH is screen height, with pX and pY being the coordinates of the location I'd like to convert.

I believe what I'm looking for is pix2Scr() not so much as code, but as an algorithm that I can add to my collection.

The proposed answer coded inline as:

Sw = 640
w = 4
Sh = 480
x = -0.5
y = 0

nx = (Sw/(2*w)) * x + (-Sw/2)
ny = (Sw/(2*w)) * -y + (-Sh/2)

console.log nx,ny

produces -360 -240 which is correct except for sign so I've put in one too many minus signs somewhere?

rschwieb
  • 160,592
hsmyers
  • 113
  • "center plus width" what does that mean? – rschwieb Jan 22 '19 at 20:54
  • "a,y coordinate on the screen plane" what does that mean? – rschwieb Jan 22 '19 at 20:54
  • "'free-standing' number" what does that mean? – rschwieb Jan 22 '19 at 20:54
  • I know that most graphics users consider the origin to be in the upper left hand corner of the screen, the x axis on the horizontal, and the y axis headed down on the vertical, which makes a left-handed coordinate system. Are you saying you want to take the regular representation of the complex plane in right-handed x-y coordinates and transform it into this one? or perhaps a screen coordinate system with the origin in the center of the screen? – rschwieb Jan 22 '19 at 20:57
  • 3
    Your paragraph is filled with sentences that manage not to describe what you want. A few examples of what you expect to map where would be useful. – rschwieb Jan 22 '19 at 20:58
  • 1
    Fractal images are often presented with a complex center coordinate plus a width (width in the complex plane). The screen plane has an xy origin in the upper left of o,o with numbers increasing from there. Free-standing was a reference that C in a typical mandelbrot calculation involves a combination of factors before iteration. Here, it comes from user input i.e. free-standing. I'm asking for a conversion of the number from one plane to the other. My view of the complex plane is always bounded by -2.5, -1.5i and 1.5, 1.5i the screen plane is bounded by o,o and 640,480 (varies with image size) – hsmyers Jan 22 '19 at 21:34
  • 1
    as an example: -0.759 +0.000i @ +2.500 decoded center and width Robert P. Munafo uses them in his 'Encyclopedia' to locate his images. In particular, "Sea-Horse Valley"…

    https://mrob.com/pub/muency/seahorsevalley.html

    – hsmyers Jan 22 '19 at 21:41
  • If you can show the details of your working "screen to complex" conversion, perhaps someone will be able to invert it for you. It is better to edit your question to add this additional information than have it buried in the comments. – Claude Jan 23 '19 at 12:39
  • I have voted to close this question as unclear. At the risk of being rude, I find your question utterly impenetrable, and your comments do nothing to clarify. As has already been suggested, could you please edit your question to include some examples? – Xander Henderson Jan 23 '19 at 15:06
  • @XanderHenderson I completely disagree. The initial question was indeed somewhat impenetrable, but the comments seemed to completely clarify everything for me, modulo a detail about what "width" specified. – rschwieb Jan 23 '19 at 15:25
  • @hsmyers . But yes, Xander's suggestion to include examples in the post (and not only in the comments) would be a good improvement, if you make the time for this. – rschwieb Jan 23 '19 at 15:33

1 Answers1

2

First, let me clarify my interpretation of the problem. Your additional context was a great help in getting this far, but I may have misunderstood.

Let the location to be displayed be represented by $x_0+iy_0\in\mathbb C$ and $w\in\mathbb R$. My understanding is that $w$ represents a positive distance so that we want to map

  • from a rectangle lying between the vertical lines described by $x=x_0-w/2$ and $x=x_0+w/2$ centered on $x_0+iy_0$

  • to screen coordinates so that the image of $x_0+iy_0$ is centered in the screen and the picture fills the screen (aspect ratio preserved)


Three basic things need to happen

  1. Scale the plane;
  2. Reflect the plane over the $x$ axis, since the display uses left-handed coordinates; and
  3. translate the image of $x_0+iy_0$ to the center of the display.

Tackling 1) first: Let $S_w$ and $S_h$ denote the number of pixels in the width and height of the image, respectively. To make a rectangle that used to be $2w$ wide fit in a space that is $S_w$ pixels wide, we'll scale by a factor of $\lambda =\frac{S_w}{w}$.

Now 2): So far, we've mapped the complex plane into a (right-handed) pixel plane. To reverse the direction of the $y$ axis, we make the appropriate transformation. If you are thinking of things in terms of complex mappings, that is what the complex conjugate map does, and if you are thinking in terms of linear transformations on $\mathbb R\times\mathbb R$ then you would use the matrix $\begin{bmatrix}1&0\\0&-1\end{bmatrix}$ .

Now 3): If you were to use these transformations right now and map the whole plane to your screen, all you'd see is an image of quadrant $IV$ of the complex plane. The center that you want ($p_2=\lambda(x_0, -y_0)$ in the new coordinates) is probably nowhere in sight. Centered on your screen is the point $p_1=(S_w/2, S_h/2)$, by definition.

So the requisite translation is to add $p_1-p_2$ to each point of the screen plane.

I'll write out the whole transformation now, all in terms of $w,x_0, y_0, S_w,S_h$:

$$ (x,y)\mapsto \frac{S_w}{w}(x,-y)+(-\frac{S_w}{w}x_0+\frac{S_w}{2},\frac{S_w}{w}y_0+\frac{S_h}{2}) $$

In the example you gave with $w=4$, $x_0=-1/2$ and $y_0=0$, $S_w=640$ and $S_h=480$, this turns into

$$ (x,y)\mapsto 160(x,-y)+(400,240) $$

This maps $(-1/2,0)$ to $(320, 240)$,

and $(-5/2,0)$ to $(0, 240)$,

and $(3/2,0)$ to $(640, 240)$,

as expected.

Or, to rewrite it in terms of $z\in\mathbb C$ where $z_0=x_0+iy_0$:

$$ z\mapsto \frac{S_w}{w}\overline{(z-z_0)}+\frac{S_w}{2}+i\frac{S_h}{2} $$


Another way to do this would have been to use four-point correspondence to compute the affine transformation. It is a very general algorithm which can compute the affine transformation needed to map from one plane in $3$-space to another plane.

Actually for an affine transformation like this one, only three points are necessary because things simplify a little since you are moving around the same plane.

rschwieb
  • 160,592
  • 1
    I think I got everything straight. It is always a pain for me switching handedness in coordinate systems, but this deserves testing. Please tell me if your implementation doesn't perform as advertised, and I'll see what I can do to help. – rschwieb Jan 23 '19 at 15:28
  • My first fractal software in the '80s went one entire iteration displaying top inverted with bottom before a good friend pointed it out! – hsmyers Jan 23 '19 at 19:42
  • In going over your answer, I believe you are correct in all essentials. I may stumble given my mathematical ignorance, but I'm used to that… – hsmyers Jan 23 '19 at 19:49
  • I've added an edit with my first version of your answer. I get both back as negative numbers so I've misplaced a minus sign or two? – hsmyers Jan 23 '19 at 20:12
  • @hsmyers Is the width given with the point the width of the entire window or just one half? From the example you gave at the time, I was led to believe it was one half (and that you were supposed to go $\pm$ that amount in either direction.) – rschwieb Jan 23 '19 at 21:14
  • @hsmyers I don't think I understand your scr2pix function. It appears to take in 8 constants and spit out two numbers. Don't you need all these numbers to determine the transformation? The transformation would be determined by more than two numbers... I was expecting two more variables $x,y$ to represent the coordinates of anything from the plane. – rschwieb Jan 23 '19 at 21:42
  • @hsmyers I also wrote my answer thinking you wanted to preserve the aspect ratio of the picture... but this appears to not be the case? – rschwieb Jan 23 '19 at 21:44
  • @hsmyers See, you are coding $w=4$ to be the width of the entire window in the complex plane... my earlier question about this was whether or not it should be $4$ or $2$ in this case. The answer still doesn't look right but... I'll keep checking the math. – rschwieb Jan 23 '19 at 21:52
  • Good points all. The 4 I used as w is the width of the complex plane where -0.5,0 is the center, i.e. the typical image of the entire mandelbrot set is 4 wide and high. I don't use scr2Pix in the normal course of the program—it's created stand alone as a utility. I'll have to think about my failure to think about aspect ratio…normally that is taken care of in the existing code and I don't think about. The width is bound tto the image with the C given as it's center. – hsmyers Jan 23 '19 at 22:13
  • @hsmyers I corrected the equations to use $w$ as the width of the entire image. But that makes your center map to $(-400, -240)$ now, which apparently still has the same problem as before (and perhaps doesn't agree with your figuring?) – rschwieb Jan 24 '19 at 12:45
  • @hsmyers Actually now that I look at it, it seems I should have written $p_1−p_2$! That might account for the sign difference. – rschwieb Jan 24 '19 at 14:52
  • 1
    @hsmyers I think we have the correct map now. – rschwieb Jan 24 '19 at 14:59
  • What values are X and Y? X0 and Y0 are explained, but obviously, X can't equal X0 or the function would reduce to (320,240) regardless of input. – hsmyers Jan 25 '19 at 21:46
  • @hsmyers $(x,y)$ are the coefficients of an arbitrary complex input $x+iy$. The whole thing can be rewritten with complex arithmetic... – rschwieb Jan 25 '19 at 23:06
  • Note that I'm talking about the Sw/2w(x,-y) not the leading (x,y) ->. – hsmyers Jan 25 '19 at 23:43
  • @hsmyers I am too. I don't follow what the objection is, if you have one.. – rschwieb Jan 26 '19 at 00:03
  • I just don't understand what values to use for that x and y. – hsmyers Jan 26 '19 at 03:13
  • @hsmyers In the interlude I've also rewritten the two equations in terms of complex numbers. You asked for a map that converts the complex plane into your screen plane. That's exactly what I'm describing. A function that takes a point in the complex plane (described as a pair of reals or as a single complex number) and spits out the coordinates in the screen plane. – rschwieb Jan 26 '19 at 13:54
  • @hsmyers Everything in the complex plane looks like $x+iy$. There is $x$. There is $y$. The image in the screen is computed using the function I described. – rschwieb Jan 26 '19 at 13:55
  • Are you saying that in (x,y)↦160(x,−y)+(400,240) that (400,240) should be a constant? Always apply my complex point to the above without calculating a new result for (400,200)? – hsmyers Jan 26 '19 at 17:40
  • @hsmyers that figure depends on the screen size and the chosen center, not on the point being mapped. Has your implementation of it been fruitful? – rschwieb Jan 26 '19 at 19:32
  • Excellent! I should have asked at the roll-up sleeve point! Thank you for tolerating my persistent ignorance! – hsmyers Jan 26 '19 at 19:49
  • @hsmyers Great! Glad to have helped. – rschwieb Jan 27 '19 at 02:53