home *** CD-ROM | disk | FTP | other *** search
Prolog Source | 1987-08-19 | 3.5 KB | 93 lines |
- /*------------------------------------------------------------*/
- /* APFELPRO.PRO */
- /* Iterative Prozeße in Prolog */
- /* veranschaulicht anhand des fraktalen "Apfelmännchens" */
- /* Dipl. Ing. B. Heimbrecht & PASCAL INTERNATIONAL */
- /*------------------------------------------------------------*/
-
- domains
- i = integer
- r = real
-
- database
- xtable(r,i) /* Die Koordinaten aller Punkte in */
- ytable(r,i,i) /* der Database ablegen. */
-
- predicates
- init
- apple(i)
- constants(i,i,r,r,r,r,i) create_all_xy(i,i,r,r,r,r)
- create_x(i,r,r,r) create_y(i,r,r,r) reflect(r,i,r)
- iteration(r,r,r,r,i,i)
- clear_database
- ok
-
- goal init.
-
- clauses
-
- /*------------------------------------------------------------*/
- /* Koordinaten aller Punkte berechnen */
- init :-
- clear_database, graphics(1, 1, 0), nl,nl,
- write(" APFELPRO \n\n",
- " Tabellen werden erstellt\n\n",
- " bitte ca. 25 sec warten"),
- constants(PXmax, PYmax, XCmin, XCmax, YCmin, YCmax, Imax),
- create_all_xy(PXmax, PYmax, XCmin, XCmax, YCmin, YCmax),
- clearwindow, apple(Imax), !.
-
- /*--------------------------------------------------------------*/
- /* Koordinaten-Punkte berechnen und in Tabelle abspeichern */
- create_all_xy(PXmax, PYmax, XCmin, XCmax, YCmin, YCmax) :-
- FX =32000/PXMax, DX =(XCmax-XCmin)/PXmax,
- FY =32000/PYMax, DY =(YCmax-YCmin)/PYmax,
- create_x(PXmax, XCmin, FX, DX), create_y(PYmax, YCmin, FY, DY).
-
- create_x(X, XCmin, FX, DX) :-
- X >0, !, Xn =X-1, XC =XCmin+DX*Xn, XP =FX*Xn,
- asserta(xtable(XC, XP)), create_x(Xn, XCmin, FX, DX)
- ; ok.
-
- create_y(Y, YCmin, FY, DY) :-
- Y > 0, !, Yn = Y-1, YC = YCmin+DY*Yn, YP = FY*Yn,
- reflect(YC, YP, DY), create_y(Yn, YCmin, FY, DY)
- ; ok.
-
- /*-- Spiegelung --*/
- reflect(YC, YP, DY) :-
- ytable(YCs, YPs, 0),
- YC+YCS = Diff, DY/2 = Tol, Diff < Tol, Diff > -Tol,
- retract(ytable(YCs,_,_)), asserta(ytable(YC, YP, YPs)), !
- ; asserta(ytable(YC, YP, 0)).
-
- /*------------------------------------------------------------*/
- /* Vereinbarung der Konstanten */
- constants(PXmax, PYmax, XCmin, XCmax, YCmin, YCmax, Imax) :-
- XCmin = -0.75, XCmax = +2.25, /* Darstellungsfenster für */
- YCmin = -1.25, YCmax = +1.25, /* das Apfelmännchen */
- Imax = 50, /* max. Zahl Iterationen */
- PXmax = 320, PYmax = 200. /* Bildauflösung in Pixel */
-
- /*------------------------------------------------------------*/
- /* Bildpunkte mit Backtracking durchlaufen, jeweils Iteration */
- /* starten und Punkt setzen. */
- apple(Imax) :-
- xtable(XC, XP), ytable(YC, YP, YPs), /* Koordinate holen */
- iteration(XC,YC, 0,0, Imax, Color), /* itrieren */
- dot(YP, XP, Color), YPs <> 0, dot(YPs, XP, Color),
- fail /* Zurück und neue Koordinate holen */
- ; readchar(_), !.
-
- /*-- Iterationsschleife, Abbruch bei Imax oder Z^2 < 100 --*/
- iteration(XC, YC, XZ, YZ, I, Color) :-
- X2 = XZ*XZ, Y2 = YZ*YZ, X2+Y2 < 100, !, I > 1,
- Xn = X2-Y2-XC, Yn = 2*XZ*YZ-YC, In = I-1,
- iteration(XC, YC, Xn, Yn, In, Color) /* rekursiv */
- ; Color = I mod 3 + 1. /* Wenn Ende, Farbe zurueckgeben */
-
- clear_database :- retract(_), fail
- ; ok.
-
- ok.