Thread: Tips&Tricks in AVS View Single Post
3rd December 2002, 19:13   #42
UnConeD
Whacked Moderator

Join Date: Jun 2001
Posts: 2,104
Atero: if you don't have anything constructive to add to someone's question, shut up please.

VisualAgnosia: the superscope is just a drawing tool... you give it (x,y) coordinates on the screen with a color if you wish.

So if you want to plot a graph of a function z=f(x,y), you'll first need to write code that traverses a grid of points in 2D rectangle, then calculates the z value for that point and then transforms the point with perspective.

The best way to do this is not to use the 'i' counter in the superscope, but make your own counters. For example, this draws a simple 20x20 grid:

init:
n=400;

per frame:
t=t-.05;px=0;py=0;ct=cos(t);st=sin(t);

per point:
gx=(px/19)*2-1; gy=(py/19)*2-1;
gz=-exp(-(sqr(gx)+sqr(gy))*4)*3+1.6;
x1=gx*ct-gy*st;y1=gx*st+gy*ct;
x=x1/(y1+3);y=gz/(y1+3);
px=if(below(px,19),px+1,0);
py=if(equal(px,0),py+1,py);
red=if(equal(px,1),0,1);green=red;blue=red;

There's a lot going on here, so I'll dissect it:

First, we have 400 points on our grid, so we set n to 400.
We'll be rotating the grid around, and we'll use 't' as the angle, which is decreased every frame by 0.05 radians. We also calculate the cosine and sine of the angle in the frame routine so we don't have to calculate it again every point, for speed reasons.
We'll be traversing a 20x20 grid, and we'll store the current grid point in (px,py). px and py will take on the values 0-19 (20 points), first x then y. We start at (0,0).

Per point, we transform (px,py) (between (0,0) and (19,19)) into (gx,gy) a point between (-1,-1) and (1,1). This is done by dividing by 19, multiplying by 2 and subtracting 1 (this piece can be done faster if we merge everything into one multiplication + add/sub, but this is easier to understand):
```code:
/19     *2      -1
0..19 -> 0..1 -> 0..2 -> -1..1
```

Next, we calculate 'gz' based on gx and gy. I use exp(-(sqr(gx)+sqr(gy))) which is an exponential 'hump' the shape of the normal gaussian distribution. We multiply it by 3 and add 1.6 to make it fit in the AVS window.

Next we rotate (gx,gy) by t radians, so that we get the point (x1,y1). We now use x1 as the x-coordinate, gz as the y-coordinate (z points up) and y1 as the depth coordinate (y points into the screen). We transform it into perspective by dividing (x,y) by z (plus a camera offset of 3).

Now all we need to do is make sure (px,py) traverse the values 0-19. First, we check per point if px is smaller than 19: if so, we add 1. If px equals 19, we reset it back to zero. If px equals zero, we increase py by 1.
This has the effect of traversion one row on the grid, and when we reach the end, we go to the next row and start over.
```code:
0  1  2  3  4  5      18 19
.  .  .  .  .  . ..... .  .  |
.  .  .  .  .  . ..... .  .  |
---------->                \|/
v
```

And finally there's a little extra: AVS draws the superscope as one chain of line segments. This means that there will be a connecting line between the different rows, which is not what we want. So, we set the color to black for that certain line segment, and white for all the rest: if you use additive or maximum blending, the black lines won't show up.

Tadaa... you now have a rotating plot.
You could duplicate the scope and reverse the meaning of gx and gy in the second copy, that way you get a grid of lines. Make sure you reload the preset to synchronize the two scopes... (result is attached)

So you see if you want to plot a graph, you have to do *everything* yourself.
Attached Files
 plot.zip (395 Bytes, 1053 views)