	PROGRAM APFEL4
C	20.11.85 WI/1
C	MANDELBROT-GRAPHIC.
C
C	FUNCTIONAL DESCRIPTION :
C
C	The color of an array of points in the complex plane is
C	evaluated by a special algorithm.The result is drawn as
C	a picture on the screen with the aid of the CORE-Graphic
C	System.The Picture-Resolution can be selected from 1...40
C	by a special parameter,e.g :
C
C		Resolution  =	 1  ->  X : Y  =  18 :  12
C		Resolution  =	10  ->  X : Y  = 180 : 120
C		Resolution  =	40  ->  X : Y  = 720 : 480
C
C	The 'color' of the Start-Value SX/SY in the Complex-Plane is
C	transformed to the origin of a window with the relative size
C	of SX+DX / SY+DY in the World-Coordinate-System.The dimensions
C	of this window are given by the selected resolution.The
C	window's origin is located at the upper-left corner.A special
C	algorithm is applied to compute the color of every point
C	(see explanation in subroutine CHAOS4) in the selected part of
C	the Complex-Plane.The picture is saved in a graphic metafile.
C
	INCLUDE 'LB:[1,5]CGL.FTN'
C
	REAL*4		SX,SY,DX,DY
	INTEGER*2	SAVE(24),ORIGIN,IFCO,N,IX,IY,FACT,NX,NY
	INTEGER*2	WHITE(3)
	REAL*4		VXM,VXP,VYM,VYP,WXM,WXP,WYM,WYP
	REAL*4		LWX,LWY,LWXO,LWYO
	LOGICAL		YES
	CHARACTER*1	ESC
	CHARACTER*10	FILNAM
	CHARACTER*6	NAMFIL
	CHARACTER*55	LE
C
	DATA		ESC/27/			!ASCII-ESC.
	DATA		FILNAM/'      .GID'/	!PICTURE FILE.
	DATA		WHITE/7,7,6/
	DATA LE/'I:       X:       Y:       W:       R:    F:           '/
C
71	FORMAT(1H+,A1,'[2J')			!DEFINE ERASE SCREEN.
72	FORMAT(1H+,A1,'[?6l')			!DEFINE CURSOR HOME.
73	FORMAT(1H$,A1,'[24;1H',' To continue,press RESUME !')
C
	IFCO=5					!DIALOG  FILECODE.
	ORIGIN=1				!ORIGIN : TOP LEFT.
	VXM=0.0					!SELECT -
	VXP=1.0					!
	VYM=0.0					!
	VYP=1.0					!VIEWPORT.
	IX=18					!BASIC X-POINTS.
	IY=12					!BASIC Y-POINTS.
C
55	WRITE(IFCO,71)ESC			!CLEAR SCREEN.
	WRITE(IFCO,72)ESC			!CURSOR HOME.
C
	WRITE(IFCO,100)
100	FORMAT(1X,'Graphical Presentation of the Mandelbrot_Set :',//)
C
5	WRITE(IFCO,'(1H$,''Number of planned iterations    (I5) : '')')
	READ(IFCO,'(I5)',ERR=5)N
C
15	WRITE(IFCO,'(1H$,''Select X-Start Position    |...10.0| : '')')
	READ(IFCO,'(F5.0)',ERR=15)SX
	IF(SX.LT.-10.0.OR.SX.GT.10.0)GOTO 15
C
25	WRITE(IFCO,'(1H$,''Select Y-Start Position    |...10.0| : '')')
	READ(IFCO,'(F5.0)',ERR=25)SY
	IF(SY.LT.-10.0.OR.SY.GT.10.0)GOTO 25
C
35	WRITE(IFCO,'(1H$,''Select Window-Edge          0...10.0 : '')')
	READ(IFCO,'(F5.0)',ERR=35)DX
	IF(DX.LT.0.0.OR.DX.GT.10.0)GOTO 35
C
	DY=DX*(FLOAT(IY)/FLOAT(IX))		!COMPUTE WINDOW-HEIGHT.
C
65	WRITE(IFCO,'(1H$,''Resolution-Factor           (1...40) : '')')
	READ(IFCO,'(I2)',ERR=65)FACT
	IF(FACT.LT.1.OR.FACT.GT.40)GOTO 65
C
45	WRITE(IFCO,'(1H$,''Picture Filename,6 characters max.   : '')')
	READ(IFCO,'(A)')NAMFIL
	FILNAM(1:6)=NAMFIL
C
	WRITE(LE(4:8),'(I5)')N			!BUILD -
	WRITE(LE(13:17),'(F5.2)')SX		!LEGEND -
	WRITE(LE(22:26),'(F5.2)')SY		!STRING -
	WRITE(LE(31:35),'(F5.2)')DX		!WITH -
	WRITE(LE(40:41),'(I2)')FACT		!INTERNAL -
	WRITE(LE(46:55),'(A)')FILNAM		!WRITE.
C
C	COMPUTE WINDOW DATA :
C
	WXM=SX					!COMPUTE -
	WXP=SX+DX				!
	WYM=SY					!
	WYP=SY+DY				!WINDOW.
C
C	INITIALIZE GRAPHIC SYSTEM :
C
	CALL CGL(GIC)				!INITIALIZE_CORE.
	CALL CGL(GIVS,FILNAM,LEN(FILNAM))	!INITIALIZE_VIEW_SUR.
	CALL CGL(GNF)				!NEW_FRAME.
	CALL CGL(GSV2,VXM,VXP,VYM,VYP)		!SET_VIEWPORT.
	CALL CGL(GSO,ORIGIN)			!SET_ORIGIN.
	CALL CGL(GSW,WXM,WXP,WYM,WYP)		!SET_WINDOW.
	CALL CGL(GRA2,WXP,WYP)			!RECTANGLE_ABS.
	CALL CGL(GMA2,WXM,WYM)			!MOVE_ABS_2.
	CALL CGL(GICM,SAVE)			!INQUIRE_COLOR_MAP.
	CALL CGL(GILW,LWXO,LWYO)		!INQUIRE_LINEWIDTH.
C
	LWX=(WXP-WXM)/(IX*FACT)			!COMPUTE -
	LWY=2.0*(WYP-WYM)/(IY*FACT)		!LINEWIDTH.
	IF(FACT.GT.20)LWY=LWY*2.0		!DUE TO INTERLACE.
C
	CALL CGL(GSLW,LWX,-LWY)			!SET_LINEWIDTH.
	CALL CGL(GSVS,FILNAM,LEN(FILNAM))	!SELECT_VIEW_SURFACE.
C
C	CHAOS COMPUTATION PART :
C
	NX=FACT*IX				!X-POINTS.
	NY=FACT*IY				!Y-POINTS.
C
	CALL CHAOS4(NX,NY,SX,SY,DX,DY,N)	!EXPLAN. SEE CHAOS4.
C
C	WRITE LEGEND :
C
	CALL CGL(GSCME,7,WHITE)			!SET_COLOR_MAP_ENTRY.
	CALL CGL(GSWI,7)			!SET_WRITING_INDEX.
	CALL CGL(GMA2,WXM,WYM)			!MOVE_ABS_2.
	CALL CGL(GT,LE,LEN(LE))			!TEXT.
C
	WRITE(IFCO,73)ESC			!RESUME MESSAGE.
C
	CALL WTRES				!WAIT FOR RESUME.
C
C	TERMINATE GRAPHIC SYSTEM :
C
	CALL CGL(GDVS,FILNAM,LEN(FILNAM))	!DESELECT_VIEW_SURFACE.
	CALL CGL(GSCM,SAVE)			!SET_COLOR_MAP.
	CALL CGL(GSLW,LWXO,LWYO)		!SET_LINEWIDTH.
	CALL CGL(GNF)				!NEW_FRAME.
	CALL CGL(GTC)				!TERMINATE_CORE.
C
	WRITE(IFCO,71)ESC			!CLEAR SCREEN.
	WRITE(IFCO,72)ESC			!CURSOR HOME.
C
	CALL YESNO(IFCO,YES,15,'A new picture ?')
	IF(YES)GOTO 55
C
	TYPE *,'APFEL4 : See YOU later !'
C
	CALL EXIT
C
	END