PROCEDURE helix; 

CONST
	minstripe = 2;
	height = 297;
	width = 210;

VAR
	steps, stepsPerWave, waves, border, wire: INTEGER; 	
	avStripe, stripe, stripeGradient, startamp, endamp, amp, gradient, pistep: REAL;
	i: INTEGER; x, y, gap: REAL;
	radio : STRING;
	
	PROCEDURE draw (x, y, stripe, gap: REAL);
	BEGIN
		IF (gap < -0.1) OR (gap > 0.1) THEN BEGIN
			PenFore(0,0,65535);
			MoveTo(border + x, height/2 - gap);
			Line(0,y+gap);
			PenFore(0,0,0);
			Line(stripe, 0);
			PenFore(0,0,65535);
			Line(0, -(y+gap));
			PenFore(0,0,0);
			Line(-stripe, 0);
			Move(0,y);
			Line(stripe, 0);
			PenFore(0,0,65535);
		END
		ELSE BEGIN
			PenFore(0,0,0);
			MoveTo(border + x, height/2);
			Line(stripe,0);
		END;
	END;

BEGIN
	
	stepsPerWave := PANZAHL_SCHRITTE;
	radio := PDOPPEL;
	startamp := PSTART_AMP;
	endamp := PEND_AMP;
	waves := PANZAHL_WELLEN;
	border := PRAND;
	
	wire := width - 2*border;
	steps := waves * stepsPerWave;
	avStripe := wire/steps;

	{korrekturen}
		{amplituden check}
		IF (Abs(startamp) > height/4) THEN BEGIN 
			endamp := endamp / (Abs(startamp)/(height/4));
			startamp := height / 4 * (startamp/startamp) {--> um Vorzeichen nicht zu verlieren};
		END;
		IF (Abs(endamp) > height/4) THEN BEGIN 
			startamp := startamp / (Abs(endamp)/(height/4));
			endamp := height / 4 * (endamp/endamp);
		END;

	gradient := (endamp - startamp)/wire;
	stripeGradient := gradient / 4;

		{steps and stripes}
		IF (avStripe < minstripe) AND (avStripe >= 0) THEN BEGIN
			avStripe := minstripe;
			steps := wire / avStripe;
			stepsPerWave := steps / waves;
			steps := stepsPerWave * waves; {falls steps / waves eine ungerade Zahl gibt, wird sie gerundet, weil stepsPerWave Integer ist --> steps muss gegebenfalls korrigiert werden.}
		END;

		IF (gradient <> 0) THEN BEGIN 
			avStripe := wire/2 * Abs(stripeGradient) + avStripe;
			steps := wire / avStripe;
			stepsPerWave := steps / waves;
			steps := stepsPerWave * waves;
			avStripe := wire / steps;
		END;

		IF (stepsPerWave MOD 2 = 1) THEN BEGIN
			stepsPerWave := stepsPerWave - 1;
			steps := stepsPerWave * waves;
			avStripe := wire / steps;
		END;

	{set drawing}
	SetUnits(1,10,0,25.400000000, 'mm', 'mm2');
	SetDrawingRect(width, height);
	SetOrigin(-width/2, -height/2);
	Layer('helix');
	
	{set pen}
	PenPat(2);
	MoveTo(0,0);
	
	{zeichnen}
	
	{draw page}
	PenFore(65535,0,0);
	Line(0,height);
	Line(width,0);
	Line(0,-height);
	Line(-width, 0);

	{haupt-falz}
	PenFore(0,0,0);
	MoveTo(0,height/2);
	Line(border,0);
	Move(wire,0);
	Line(border,0);
	
	x := 0;
	pistep := PI / stepsPerWave;
	stripe:= -wire/2 * stripeGradient + avStripe; 
	amp := startamp;
	
	IF (radio = 'doppelt') THEN BEGIN

		FOR i:=1 TO steps DO BEGIN 
			
			IF (i MOD 2 = 0) THEN BEGIN
				gap := amp + amp * cos(pistep*i - PI);
				y := amp + amp * sin(pistep*i);
			END
			ELSE BEGIN
				gap := -(amp + amp * sin (pistep*i - PI));
				y := -(amp + amp * cos(pistep*i));
			END;	

			draw(x,y,stripe,gap);	

			x := x + stripe;
			stripe := stripe + avStripe * stripeGradient;
			amp := startamp + x * gradient;
		END;
	END
	ELSE BEGIN
			
		FOR i:=1 TO steps DO BEGIN 
			
			gap := amp + amp * cos(pistep*i - PI);
			y := amp + amp * sin(pistep*i);

			draw(x,y,stripe,gap);	

			x := x + stripe;
			stripe := stripe + avStripe * stripeGradient;
			amp := startamp + x * gradient;
		END;
	END;
END;

RUN(helix);