home *** CD-ROM | disk | FTP | other *** search
Modula Implementation | 1986-11-21 | 3.4 KB | 119 lines |
- (*
- Chaos: a program that allows you to play with
- "strange attractors". See "Scientific American"
- July 1987 issue.
-
- Created: 8/10/87 by Richie Bielak
-
- Copyright (c) 1987 by Richie Bielak
-
- This program maybe freely distributed, but please leave
- my name in. Thanks.....Richie
-
- *)
- IMPLEMENTATION MODULE ChaosDraw;
-
- FROM MathLib0 IMPORT sin, cos, MathTransBase, MathTransName;
- FROM Drawing IMPORT WritePixel, SetAPen;
- FROM Intuition IMPORT WindowPtr, ITEMNUM, IntuiMessagePtr, IDCMPFlags,
- IDCMPFlagsSet, MenuNull;
- FROM Ports IMPORT ReplyMsg, GetMsg, MessagePtr;
- FROM Libraries IMPORT OpenLibrary;
- FROM SYSTEM IMPORT ADR;
- FROM Terminal IMPORT WriteString;
- FROM ChaosPanel IMPORT ControlValues;
-
- (* ++++++++++++++++++++++++++++++++++++++++++++++++++ *)
- PROCEDURE Round (x : REAL) : INTEGER;
- VAR t : INTEGER;
- BEGIN
- t := TRUNC (x);
- IF (x - FLOAT(t)) > 0.5 THEN INC (t)
- ELSIF (FLOAT(t) - x) > 0.5 THEN DEC (t)
- END;
- RETURN t
- END Round;
-
- (* ++++++++++++++++++++++++++++++++++++++++++++++++++ *)
- (* Check if there are any messages waiting for *)
- (* processing. Ignore, all but the "End" message *)
- PROCEDURE ReadyToQuit (wp : WindowPtr) : BOOLEAN;
- VAR
- msgptr : IntuiMessagePtr;
- class : IDCMPFlagsSet; code : CARDINAL;
- BEGIN
- LOOP
- msgptr := GetMsg (wp^.UserPort^);
- IF msgptr = NIL THEN RETURN FALSE END;
- code := msgptr^.Code; class := msgptr^.Class;
- ReplyMsg (MessagePtr(msgptr));
- IF (class = IDCMPFlagsSet{MenuPick}) AND (code <> MenuNull) THEN
- RETURN TRUE
- END;
- END;
- END ReadyToQuit;
-
- (* ++++++++++++++++++++++++++++++++++++++++++++++++++ *)
- PROCEDURE DrawPicture (wp : WindowPtr; w, h : CARDINAL);
-
- (* ++++++++++++++++++++++++++++++++++++++++++++++++++ *)
- PROCEDURE Plot (x, y : REAL; ZoomFact : REAL);
- VAR
- xc, yc, rc : INTEGER;
- BEGIN
- (* Convert to integers *)
- xc := Round (ZoomFact * x); yc := Round (ZoomFact * y);
- (* Translate with respect to the center of the display *)
- xc := xc + INTEGER(w) DIV 2;
- yc := yc + INTEGER(h) DIV 2;
- (* Check if within range *)
- IF (xc < 0) OR (xc > INTEGER(w) - 1) THEN
- RETURN
- ELSIF (yc < 0) OR (yc > INTEGER(h) - 1) THEN
- RETURN
- END;
- (* Now, plot the point *)
- rc := WritePixel (wp^.RPort^, xc, yc);
- END Plot;
-
- (* ++++++++++++++++++++++++++++++++++++++++ *)
- PROCEDURE DrawOrbit (a, x, y, cosa, sina, zoom : REAL);
- VAR i: CARDINAL;
- xx, t1, z : REAL;
- BEGIN
- FOR i := 1 TO ControlValues.IterPerOrbit DO
- t1 := y - x * x;
- xx := x * cosa - t1 * sina;
- y := x * sina + t1 * cosa;
- x := xx;
- Plot (x, y, zoom);
- END;
- END DrawOrbit;
-
- VAR
- a, x, y : REAL;
- i : CARDINAL;
- z, cosa, sina : REAL;
- BEGIN
- WITH ControlValues DO
- x := 0.001; y := 0.001;
- (* First compute some things that don't change *)
- cosa := cos(a); sina := sin(a);
- z := ZoomFactor * 100.0;
- FOR i := 0 TO MaxOrbits DO
- SetAPen (wp^.RPort^,1 + (i MOD MaxColors));
- DrawOrbit (a, x, y, cosa, sina, z);
- IF ReadyToQuit (wp) THEN RETURN END;
- x := x + xInc;
- y := y + yInc;
- END;
- END;
- END DrawPicture;
-
- BEGIN
- MathTransBase := OpenLibrary (ADR(MathTransName),0);
- IF MathTransBase = NIL THEN
- WriteString ("Can't open math lib...\n");
- END;
- END ChaosDraw.
-