Blogger Syntax Highliter

Friday 20 March 2009

Chunk 35... a start

[People reading the book will already have done some work with points in chunk 34]

The point is the most basic element of drawing, so, although it might seem limiting to continue to think about drawing simply in terms of plotting points, the truth is there's nothing we can't draw with them.

Even so, how could one draw a complex picture like this using only points, and not a lot of talent?

xxx
Figure 1 - leaf/fern or similar

Fortunately, it will involve some lovely maths. Fear not.

First, consider the following much simpler figure.

xxx
Figure 2 - Straight line from points

You will see how to draw lines like this later on in the course using a different approach, where an output is generated from an input using a function such as y = 2x, where y represents a vertical coordinate, and x a horizontal one. The function y = 2x simply says that whatever x is, we can multiply it by 2 to find out what y is. So, if you choose the x values 1, 2 and 3, you will get the y values 2, 4 and 6, and that means you can plot the points (1,2), (2,4) and (3,6). Straight forward.

Think about it another way. if you start at (1,2) and add 1 to the x value, and 2 to the y value, you will find the next point, which is (2,4). And if you add 1 to the x value and 2 to the y value again, you will find the point (3,6). So, it is possible to think about a set of points as a relationship between an input point and an output point. In this case we have the relationship that if the input is (x,y), the next point is (x+1, y+2). This allows you to write a simple function like this

Point straightLine(Point in) { return new Point(in.x + 1, in.y + 2); }

If you call this function several times in a loop, you can plot the resulting points:

Point p = new Point(0,0); for(int i = 0; i <> { p = straightLine(p); //get a new p based on the old one point(p.x, p.y); }

Notice that on each iteration of this loop, we get a new point based on the value of the old one. We could also write this down in a more mathematical notation as follows:

(x',y') = (x,y) + (1,2), where x' is the new value of x, and y is the new value of y. A pairs of values in brackets can also be called a matrix.

When we add groups of values in matrices, we always add values in corresponding positions, so 1 gets added to x to find x', and 2 gets added to y to find y'.

If we repeat this process using the new point (x',y') as our input, we get the next point:
(x'',y'') = (x',y')+(1,2)
And so on.

So, it is possible to specify a whole set of points using just a few lines of code. Admittedly, the resulting picture here is not very exciting, but it helps to give you the idea. In fact, some functions will lead to very uninteresting pictures indeed! For example, suppose that we write a function that performs the following arithmetic:

(x',y') = (x,y) + (0,0)

If you think about this a while, you will realise that whatever the input point is, the output point is the same. That is (x',y') = (x,y). furthermore, (x'',y'') = (x',y'). So this makes the very uninteresting picture consisting of a single pixel at the point (x,y), whatever we choose x and y to be. The point is 'stuck' at its starting place. We say it is a fixed point.

While this example has all input values as fixed points,other functions may have fixed points for only some input values. Consider, for example,

out = 3*in - 2.

When in is 1, out is also 1. If we were to put the out value back into this function, it would only compute the value 1 again. This again is a fixed point of the function. (But other points, such as an input of 2, are not fixed.)

The next bit requires a bit of a leap of faith and some hand-waving. The thing is, whilst this fixed point is very uninteresting in itself, you can write very similar functions that consists of whole sets of points that collectively make beautiful pictures. They are also called fixed points, but the sense on this occasion is of a set of points, and the fixedness of these points is within the set. For example, suppose we have the set of points, let's call it P = {(1,2), (2,5) , (3,6)} and a pseudo Processing function

Point magic(Point in) { if (the input is in P) { return another point in P; } else { return a point closer to the points in P; }
}

Now it should be possible to have a really interesting or beautiful set of points P. Furthermore, we should be able to pick a point (any point, nothing up the sleeve) and "pretty soon" we should get a point back that is in the set P. Once we do have a point in P, we can start plotting them, and then we should get a really interesting or beautiful picture.

It may appear, and you'd be right, that I have still avoided the crucial question of how you come up with the interesting set of points P. How, for example, would one get a set of points to create a picture of a leaf?

This is the bit you will have to take on faith, unless you would like to explore the maths more deeply. There is a theorem, called the collage theorem (magicked by a magician called Barnsley), which tells you how to construct such a set of points. The trick is that you have first to imagine the shape you want to draw, and then imagine how you would twist, turn, shrink or grow that shape, in order to make copies of itself that fit on top of itself. What? Did I just say that? Yes, a bit like this:

xxx
Figure 3 A picture we can make some sense of in terms of collage

And now I probably need to use IExplorer as blogger won't let me load images in firefox today...

It is getting a little trickier than I thought it might. At least, how will readers write their own IFS without understanding collage.... does it matter... should I do something simpler like a random walk?

Some of this discussion probably would be better off in the previous chunk.

No comments:

Post a Comment