
/*
 *  Diffraction
 */

/*  This section of the program contains the includes.  These are other
    source files that are used by the compiler when compiling the program.
    in most instances they are included with your compiler.  */

/*  Dif.c was originally compiled under SAS/C v6  */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <dos.h>

#include <intuition/intuition.h>
#include <graphics/gfx.h>

#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>


/*  In this section of the program (below) I choose what type of screen
    I want the image to be drawn on.  I have chosen a 2 colour, Hires
    screen for this purpose.  The title of the screen is "Diffraction
    Demo".  */

struct NewScreen screendefs=
	{
	0, 0, 640, 256, 1,
	1, 0,
	HIRES,
	CUSTOMSCREEN,
	NULL,
	"Diffraction Demo",
	NULL,
	NULL
	};
	
/*  This line is simply a variable declaration  */
	
struct Screen *screen;

void main( void )
	{
	int x[3], y[3], k, i, j, a;
	double m, s, q, r;
	
	/*  Open the screen that I previously defined  */
	screen = OpenScreen( &screendefs );
	
	/*  Setup diffraction point co-ordinates  */
	/*  That means that these variables are set, to indicate where the 
	    points are that the interferance originates from.  If you change
	    these and recompile, you will get a different picture  */
	    
	x[0] = 100;
	x[1] = 300;
	x[2] = 580;
	
	y[0] = 50;
	y[1] = 150;
	y[2] = 225;
	
	
	/*  Here we start the loops.  We loop for every point on the screen
	    (X first, then Y), and then we loop for each point of interferance
	    ( variable k ).  */
	
	for( i = 0; i < 256; i++ )
		{
		for( j = 0; j < 640; j++ )
			{
			m = 0;
			for( k = 0; k < 3; k++ )
				{
				/*  Do some calculations for this point of interferance  */
				
				q = i - y[k];
				r = j - x[k];
				s = sqrt( q*q + r*r );
				m+= ( -0.2*s+50) * (1 + cos(0.1*s) );
				}
				
			/*  Calculate the colour for this point( j, i )  */
			a = ((m + 0.5) / 8);


			/*  Set the pen to use  */
			if( ( ( a%2 ) * 2 + 0.1) * 7  == 0.7 )
				SetAPen( &(screen->RastPort), 1 );
			else
				SetAPen( &(screen->RastPort), 0 );

			/*  Draw the pixel on the screen at ( j, i )  */

			WritePixel( &(screen->RastPort), j, i );
			
			/*  If a CONTROL-C is hit - exit  */
			chkabort();
			
			/*  And loop back for the next pixel  */
			}
		}
	}
