Esempio n. 1
0
int main(int argc, char *argv[]) {
  Image *src;
  const int rows = 600;
  const int cols = 800;
  const int Resolution = 50;
	Color white;
  Color Grey;
  Color dkGrey;
  Color Red;
  Color Blue;
  Point unitCircle[Resolution];
  Point unitSquare[4];
  Point pt[Resolution];
  Point ptt[Resolution];
  int i, j, index = 0;
  Matrix VTM, GTM, LTM;
  Polygon *ship[50];
  Color shipColor[50];
  double theta = 0.0;
  double phaserAngle = 0.0;
  int firePhase = 0;

  color_set(&Grey, 180/255.0, 180/255.0, 183/255.0);
  color_set(&dkGrey, 140/255.0, 140/255.0, 143/255.0);
  color_set(&Red, 250/255.0, 40/255.0, 40/255.0);
  color_set(&Blue, 30/255.0, 20/255.0, 250/255.0);

  if(argc > 1) {
    theta = atoi(argv[1]);
  }
  printf("Drawing ship with orientation %.2f degrees\n", theta);

  if(argc > 2) {
    phaserAngle = atoi(argv[2]);
    firePhase = 1;

    printf("Drawing phaser with angle %.2f degrees\n", phaserAngle);
  }

  srand(42);

  src = image_create(rows, cols);
	color_set(&white, 1.0, 1.0, 1.0);
	for(i=0; i<rows; i++){
		for(j=0; j<cols; j++){
			if((rand()%50) == 13){
				image_setColor(src, i, j, white);
			}
		}
	}

  // initialize the three matrices
  matrix_identity(&VTM);
  matrix_identity(&GTM);
  matrix_identity(&LTM);

  // Fix world coordinates as normal (x, y) 

  // give the view window an origin at -180m, -150m
  // size is a 4x3 ratio
  // VTM = T(0, rows-1)S(cols/vx, rows/vy)T(180, 150)
  matrix_translate2D(&VTM, 120, 100);
  matrix_scale2D(&VTM, cols/(4*60), -rows/(3*60));
  matrix_translate2D(&VTM, 0, rows-1);
  printf("VTM\n");
  matrix_print(&VTM, stdout);

  // make a space ship oriented along the positive X axis
  // use the LTM to move simple primitives into place
  // use the GTM to rotate the ship
  // use the VTM to change the view

  // make a list of points that form the unit circle
  for(i=0;i<Resolution;i++) {
    point_set2D(&(unitCircle[i]), 
    cos( i * 2.0 * M_PI / (float)Resolution), 
    sin( i * 2.0 * M_PI / (float)Resolution));
  }
  // set up the unit square
  point_set2D(&(unitSquare[0]), 0, 0);
  point_set2D(&(unitSquare[1]), 1, 0);
  point_set2D(&(unitSquare[2]), 1, 1);
  point_set2D(&(unitSquare[3]), 0, 1);

  // build a set of polygons that form the ship in model space
  // put the origin of the model between the engines

  // outline for the main disk
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 31, 31);
  // move it 20m along the X-axis
  matrix_translate2D(&LTM, 60, 0);
  // transform the circle points using LTM
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(Resolution, pt);
  shipColor[index++] = Red;

  printf("Post-LTM\n");
  polygon_print(ship[0], stdout);

  // main disk
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 30, 30);
  // move it 20m along the X-axis
  matrix_translate2D(&LTM, 60, 0);
  // transform the circle points using LTM
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(Resolution, pt);
  shipColor[index++] = Grey;

  // central bridge disk
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 10, 10);
  // move it 20m along the X-axis
  matrix_translate2D(&LTM, 60, 0);
  // transform the circle points using LTM
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(Resolution, pt);
  shipColor[index++] = dkGrey;

  // make the body disk elongated along the X axis
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 30, 12);
  matrix_translate2D(&LTM, 2.5, 0);

  // transform the circle points using LTM
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(Resolution, pt);
  shipColor[index++] = Grey;
  
  // make a trapezoidal strut out of the unit square
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -0.5, 0.0);
  matrix_scale2D(&LTM, 10, 10);
  matrix_shear2D(&LTM, .2, 0.0);

  for(i=0;i<4;i++) {
    matrix_xformPoint(&LTM, &(unitSquare[i]), &(pt[i]));
  }

  // move the strut out from the origin along the Y axis
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, 0, 12);

  for(i=0;i<4;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(4, ptt);
  shipColor[index++] = Grey;

  // place the second strut
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 1, -1);
  matrix_translate2D(&LTM, 0, -12);

  for(i=0;i<4;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(4, ptt);
  shipColor[index++] = Grey;

  // create an engine outline from the unit circle
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 31, 6);

  // make the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // send one engine to the right location
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -5, 27);

  // move the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(Resolution, ptt);
  shipColor[index++] = Blue;

  // send the other engine to the right location
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -5, -27);

  // move the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(Resolution, ptt);
  shipColor[index++] = Blue;

  // create an engine
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 30, 5);

  // make the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // send one engine to the right location
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -5, 27);

  // move the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(Resolution, ptt);
  shipColor[index++] = Grey;

  // send the other engine to the right location
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -5, -27);

  // move the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(Resolution, ptt);
  shipColor[index++] = Grey;

  // set up the phaser
  if(firePhase) {
    matrix_identity(&LTM);

    matrix_scale2D(&LTM, 100, 2);

    // orient the phaser
    matrix_rotateZ(&LTM, cos(phaserAngle*M_PI/180.0), sin(phaserAngle*M_PI/180.0));
    
    // translate it to the center of the disk and out
    matrix_translate2D(&LTM, 
           60 + 30 * cos(phaserAngle*M_PI/180.0), 
           30 * sin(phaserAngle*M_PI/180.0) );

    // use the unit square
    for(i=0;i<4;i++) {
      matrix_xformPoint(&LTM, &(unitSquare[i]), &(pt[i]));
    }
    
    // add the polygon
    ship[index] = polygon_createp(4, pt);
    shipColor[index++] = Red;
  }

  matrix_rotateZ(&GTM, cos(theta*M_PI/180.0), sin(theta*M_PI/180.0));

  printf("GTM:\n");
  matrix_print(&GTM, stdout);

  printf("Pre-GTM/VTM\n");
  polygon_print(ship[0], stdout);

  for(i=0;i<index;i++) {

    // multiply the polygon by the global transform matrix
    matrix_xformPolygon(&GTM, ship[i]);
    if(i==0) {
      printf("Pre-VTM\n");
      polygon_print(ship[i], stdout);
    }

    // multiply the polygon by the view transformation matrix
    matrix_xformPolygon(&VTM, ship[i]);

    if(i==0) {
      printf("Pre-draw\n");
      polygon_print(ship[i], stdout);
    }

    // draw the polygon
    polygon_drawFill(ship[i], src, shipColor[i]);
  }

  image_write(src, "space.ppm");


  image_free(src);

  return(0);
}
Esempio n. 2
0
int main(int argc, char *argv[]) {
  const int rows = 100;
  const int cols = 100;
  const int nFrames = 16;
  // View3D view3D;
  View2D view;
  //Matrix vtm;
  Matrix vtm, gtm, ltm;
  Polygon poly[16];
  Point vp[4];
  FILE *fp;

  fp = fopen("matrix_info.txt","w");

  Image *src;
  int i, j, t;
  char filename[256];

  Color color[6];
  
  // set some colors
  color_set( &color[0], 0, 0, 1 ); // blue
  color_set( &color[1], 0, 1, 0 ); // green
  color_set( &color[2], 1, 0, 0 ); // red
  color_set( &color[3], 1, 0, 1 ); // magenta
  color_set( &color[4], 0, 1, 1 ); // cyan
  color_set( &color[5], 1, 1, 0 ); // yellow

  // optional theta value
  // if(argc > 1) {
  //   theta = atoi(argv[1]);
  // }

  // initialize the three matrices
  matrix_identity(&vtm);
  matrix_identity(&gtm);
  matrix_identity(&ltm);
  // create image
  src = image_create( rows, cols );

  srand ( time(NULL) );

  for (i=0; i<16; i++){
    for (j=0; j<4; j++){
      point_set2D(&(vp[j]), rand()%cols, rand()%rows);
    }
    poly[i] = *(polygon_createp(4, vp));
  }

  // grab command line argument to determine viewpoint
  // and set up the view structure
  if( argc > 1 ) {
    float alpha = atof( argv[1] );
    if( alpha < 0.0 || alpha > 1.0 ){
      alpha = 0.0;
    }
    point_set1( &(view.vrp), 3*alpha, 2*alpha, -2*alpha - (1.0-alpha)*3 );
  }
  else {
    point_set1( &(view.vrp), 3, 2, -2 );
  }

  vector_set( &(view.x), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2] );
  view.dx = 1;  // focal length
  view.screenx = cols;
  view.screeny = rows;

  matrix_setView2D( &vtm, &view );
  matrix_print(&vtm, fp);
  // create image in arbitrary world coordinates
   
  for(t=0;t<nFrames;t++) {
    
    setWhite( src );

    for (i=0; i<16; i++){
      // need to add print statements to follow the ltm and polygons
      // Polygons not drawing
      matrix_identity(&ltm);
      matrix_translate2D(&ltm, -vp[0].val[0], -vp[0].val[1]);
      matrix_shear2D(&ltm, t, 0);
      matrix_translate2D(&ltm, vp[0].val[0], vp[0].val[1]);
      matrix_xformPolygon(&ltm, &poly[i]);
      matrix_xformPolygon(&vtm, &poly[i]);
      printf("begin scanline fill...\n");
      polygon_drawFill(&poly[i], src, color[i%5]);
      printf("...end scanline fill\n");
    }
    printf("hello: %d\n", t);
    sprintf(filename, "test5vt-%04d.ppm", t );
    image_write( src, filename );

    // translate the view across the scene
    point_set2D( &(view.vrp), 1.8 - 2.4*(t+1)/nFrames, 1.8 - 2.4*(t+1)/nFrames );
    matrix_setView2D( &vtm, &view );
  }
  fclose(fp);
  system("convert test5vt-*.ppm test5vt.gif");
  //system("rm test5vt-*.ppm");
}
Esempio n. 3
0
void ellipse_draw(Ellipse *e, Image *src, Color p){
  /* draw an ellipse into src using color p*/
  double x, y, px, py, pz;
  double xCenter, yCenter;
  double Rx, Ry;

  /*set center values */
  xCenter = e->c.val[0];
  yCenter = e->c.val[1];

  /*Major axis: Rx*/
  Rx = e->ra;
  /*Minor axis: Ry*/
  Ry = e->rb;

  /*when you are not trying to rotate the ellipse you will be using Bresenhan's
    Algorithm. It considers the cartesian equation for an ellipse and uses an 
    updated error term to decide on which pixel to draw up or to the side. We 
    will start drawing in the third quadrant to help account for pixel error*/
   /*This algorithm, for when a = 0 comes from: ellipseMidpoint, Chapter 3, pg 109-110*/ 
  if(e->a == 0){
    /*set initial x and y values*/
    x= -1; 
    y= -Ry;

    /*initial starting error terms!*/
    px = 2*Rx*Ry;
    py = 2*Rx*Rx*-y;
 
    printf("x: %f y: %f \n", x,y);
    printf("px: %f py: %f\n", px, py);
    printf("Major Axis: %f Minor Axis: %f\n", Rx, Ry);

    /*set up p, this is necessray because we have to draw an entire 
      quadrant of the ellipse. The symmetry here is not as nice as in a 
      circle*/
    pz = (Ry*Ry-(Rx*Rx*Ry)+(Rx*Rx*0.25)+Ry*Ry+py);
 
    /*this sets the color of the initial points*/
    image_setColor(src, xCenter+x, yCenter+y, p);
    image_setColor(src, xCenter-x-1, yCenter-y-1, p);

    image_setColor(src, xCenter+x, yCenter+y, p);
    image_setColor(src, xCenter-x-1, yCenter+y, p);
    image_setColor(src, xCenter+x, yCenter-y-1, p);
    image_setColor(src, xCenter-x-1, yCenter-y-1, p);

    image_setColor(src, xCenter+x, yCenter+y, p);
    image_setColor(src, xCenter-x-1, yCenter+y, p);
    image_setColor(src, xCenter+x, yCenter-y-1, p);
    image_setColor(src, xCenter-x-1, yCenter-y-1, p);


    /*wile the points are still below the tangent line with a -1 slope value
      decrement over x*/
    printf("Starting the first half of the ellipse\n");
    while (px<py){
      x--; // decrement x
      px = px+ 2*Ry*Ry;
      if (pz<0){
	pz += Ry*Ry+px;
      }else{
	y++;// increment y
	py = py- 2*Rx*Rx;
	pz += Ry*Ry +px-py;
      }
      /*set the pixel color in the 4 quadrants*/
      image_setColor(src, xCenter+x, yCenter+y, p);
      image_setColor(src, xCenter-x-1, yCenter+y, p);
      image_setColor(src, xCenter+x, yCenter-y-1, p);
      image_setColor(src, xCenter-x-1, yCenter-y-1, p);
    }

    /* when the points pass the tangent line with slope of -1 now incrementing 
       over y*/
    pz = Ry*Ry *(x*x +x) +Rx*Rx* (y*y-2*y +1) -Rx*Rx *Ry*Ry +Rx*Rx -py;

    printf("Starting the second half of the ellipse\n");
    while (y<0){
      y++;//increment y
      py = py - 2*Rx*Rx;
      if (pz>0){
	pz += Rx*Rx-py;
      }else{
	x--;//decrement x
	px +=  2*Ry *Ry;
	pz += Rx*Rx-py+px;
      }
      /*set the pixel color in the 4 quadrants*/

      image_setColor(src, xCenter+x, yCenter+y, p);
      image_setColor(src, xCenter-x-1, yCenter+y, p);
      image_setColor(src, xCenter+x, yCenter-y-1, p);
      image_setColor(src, xCenter-x-1, yCenter-y-1, p);
    }
    printf("YAY you have drawn an ellipse at angle 0\n");

  }else{

    printf("You are rotating the ellipse\n");
    /*We are going to use a different method to rotate the ellipse*/
    /*First: we will build a list of points on the boundary of the ellipse
      where at the further regions there are more points and we will ultimately
      connect the lines together to make an ellipse.*/

    Point *holdPoints; 
    Point pt;
    double rmax, numPoints, stepSize;
    double x1, y1, x2, y2;
    double xR1, yR1, xR2, yR2, step;
    double angle;
    Line l1;
    int k, m;
    
    /*pick which is bigger the Major axis or the Minor axis*/
    if (Rx >Ry){
      rmax = Rx;
    }else{
      rmax = Ry;
    }

    /*convert the angle to polar coordinates*/
    angle = (e->a)*(PI/180);
    printf("angle: %f\n", angle);

    /*determine the step size based on the fact that we will have Rmax number 
     of points*/
    stepSize = 1/(rmax);
    numPoints = rmax*0.5;

    /*malloc space for an array of Points*/
    holdPoints = malloc(sizeof(Point)*(numPoints));
    
    printf("number of points %f\n", numPoints);
    printf("size of a step %f\n:", stepSize);
    
    /*get the initial point*/
    x1 = Rx*cos(0);
    y1 = Ry*sin(0);
    point_set2D(&pt, x1,y1);

    /*add that point to the list*/
    holdPoints[0] = pt;

    printf("x1: %f y1: %f\n", x1, y1);

    /*loop through the number of points and deterine the point value for 
      the incremented step size*/
    for(k = 1; k<numPoints; k++){
      step = stepSize*(double)k;
      x2 = (holdPoints[k-1].val[0] * cos(step))-((Rx/Ry)*holdPoints[k-1].val[1]*sin(step));
      y2 = ((Ry/Rx)*holdPoints[k-1].val[0]*sin(step))+(holdPoints[k-1].val[1]*cos(step));
      point_set2D(&pt, x2, y2);
      
      holdPoints[k]=pt;
    }

    /*breaking down matrix math into two equations apply a rotation and translation to the list of points 
      and draw the points at there new location connecting the lines between them*/
    for(m=0; m+1<numPoints; m++){
      xR1 = holdPoints[m].val[0]*cos(angle)+holdPoints[m].val[1]*sin(angle)*-1 +xCenter;
      yR1 = holdPoints[m].val[0]*sin(angle)+holdPoints[m].val[1]*cos(angle)+yCenter;
      xR2 = holdPoints[m+1].val[0]*cos(angle)+holdPoints[m+1].val[1]*sin(angle)*-1 +xCenter;
      yR2 = holdPoints[m+1].val[0]*sin(angle)+holdPoints[m+1].val[1]*cos(angle)+yCenter;
      line_set2D(&l1, xR1, yR1, xR2, yR2);
      line_draw(&l1, src, p);
    }
    /*free malloced space for the holdPoints array*/
    free(holdPoints);
  }

}
Esempio n. 4
0
// draw some random lines, then two boxes
int main(int argc, char *argv[]) {
  const int nPoints = 1000;

  Polyline thing1;
  Polyline *thing2;
  Point p[nPoints];
  int i;
  int rows = 500;
  int cols = 500;
  Image *src;
  Color Blue;
  Color Red;
  Color Green;

  Color_set( &Blue, 0.1, 0.15, 0.7 );
  Color_set( &Red, 0.8, 0.2, 0.1 );
  Color_set( &Green, 0.2, 0.6, 0.2 );
  
  printf("Startup\n");
  for(i=0;i<nPoints;i++) {
    point_set2D( &(p[i]), drand48()*cols/2, drand48()*rows/2 );
  }

  // init is necessary, because otherwise the fields are full of garbage
  printf("Polyline init and set\n");
  polyline_init( &thing1 );
  polyline_set( &thing1, 20, &( p[42] ) );

  printf("Polyline create\n");
  thing2 = polyline_createp( 10, &(p[105] ) );
  
  printf("Creating image\n");
  src = image_create( rows, cols );

  printf("Drawing poly-lines\n");
  // draw some random lines
  polyline_draw( &thing1, src, Blue );
  polyline_draw( thing2, src, Red );

  // draw a box going counter-clockwise, should be ok
  point_set2D( &(p[500]), 50, 300 );
  point_set2D( &(p[501]), 50, 350 );
  point_set2D( &(p[502]), 100, 350 );
  point_set2D( &(p[503]), 100, 300 );
  point_set2D( &(p[504]), 50, 300 );

  printf("Counter-clockwise box\n");
  polyline_set( thing2, 5, &(p[500]) );
  polyline_draw( thing2, src, Green );

  // draw a box going clockwise, should not be ok
  point_set2D( &(p[500]), 350, 300 );
  point_set2D( &(p[501]), 400, 300 );
  point_set2D( &(p[502]), 400, 350 );
  point_set2D( &(p[503]), 350, 350 );
  point_set2D( &(p[504]), 350, 300 );

  printf("Clockwise box\n");
  polyline_set( thing2, 5, &(p[500]) );
  polyline_draw( thing2, src, Green );

  image_write( src, "test3c.ppm");

  printf("Cleanup\n");
  polyline_clear( &thing1 );
    printf("clear\n");
  polyline_free( thing2 );
    printf("free\n");
  image_free( src );

  return(0);
}
Esempio n. 5
0
int main(int argc, char *argv[]){
  Image *src; 
  const int rows = 500; 
  const int cols = 500; 
  Circle circ; 
  Color white, yellow, blue,red, sky;
  Point p;
  Point pt[50];
  Polygon *poly;
  int i,j;

  color_set(&yellow, 1.0, 1.0, 0.2);
  color_set(&blue, 0.0, 0.2, 0.4);
  color_set(&red, 0.2,0.0, 0.0);
  color_set(&white, 1.0, 1.0, 1.0);
  color_set(&sky,0.4 ,0.6 , 1.0);

  src = image_create(rows, cols);

 for(i = 0; i< rows; i++) {
    for(j = 0; j<cols; j++){
      image_setColor(src, i, j, sky);

    }
  }

  for(i=0;i<50;i++) {
    float dr = rand() % 15;
    
    point_set2D(&(pt[i]), dr, dr+10);
      circle_set(&circ, pt[i] , 10);
      circle_drawFill(&circ, src, white);

  }



 point_set2D(&(pt[0]), 210, 340);
 point_set2D(&(pt[1]), 280, 340 );
 point_set2D(&(pt[2]), 280, 410 );
 point_set2D(&(pt[3]), 210, 410 );

 poly = polygon_createp(4, pt);
 polygon_drawFill(poly,src, red);

 color_set(&yellow, 0.8, 0.8, 0.0);
 point_set2D(&(pt[0]), 215, 345);
 point_set2D(&(pt[1]), 275, 345 );
 point_set2D(&(pt[2]), 275, 405 );
 point_set2D(&(pt[3]), 215, 405 );

  poly = polygon_createp(4, pt);
  polygon_drawFill(poly,src, yellow);

  printf("writing output\n");
  image_write(src, "minion.ppm");

  image_free(src);

  return(0);
}
Esempio n. 6
0
int main(int argc, char *argv[]) {
  Point start[NUMLINES], end[NUMLINES];
  Color color[NUMLINES];
  Image *src;
  Line line;
  long i,j;
  Color white;
  double tstart, tend;
  long numLines;
  double sum, dx, dy;
  struct timeb tp;

  color_set( &white, 1.0, 1.0, 1.0);

  // allocate an image ROWS by COLS
  src = image_create(ROWS, COLS);
  if(!src) {
    fprintf(stderr, "unable to allocate memory\n");
    exit(0);
  }

  // Initialize the image to all white
  for(i=0;i<src->rows;i++) {
    for(j=0;j<src->cols;j++) {
      image_setColor(src, i, j, white );
    }
  }

  // Pre-calculate the line endpoints and colors so we don't have to
  // call the random() function inside the main drawing loop.
  sum = 0.0;
  for(i=0;i<NUMLINES;i++) {
    int tsx, tsy, tex, tey;
    tsx = (int)(drand48() * COLS);
    tsy = (int)(drand48() * ROWS);
    tex = (int)(drand48() * COLS);
    tey = (int)(drand48() * ROWS);
    point_set2D(&(start[i]), tsx, tsy );
    point_set2D(&(end[i]), tex, tey );
    color[i].c[0] = drand48();
    color[i].c[1] = drand48();
    color[i].c[2] = drand48();

    dx = tsx - tex;
    dy = tsy - tey;
    sum += sqrt(dx * dx + dy * dy);
  }
  sum /= NUMLINES;
  printf("Average line length = %.1lf\n", sum);

  // Start drawing lines
  ftime( &tp );
  tstart = tp.time + tp.millitm / 1000.0;

  for(i=0,numLines=0; numLines < 5000000;numLines++, i++) {
    i = i % NUMLINES;
    line_set(&line, start[i], end[i]);
    line_draw(&line, src, color[i]);
  }

  ftime( &tp );
  tend = tp.time + tp.millitm / 1000.0;

  // print out the result
  printf("%.2lf lines per second\n", numLines / (tend - tstart) );

  // write out the image
  image_write(src, "lines.ppm");

  // free the image data
  image_free(src);

  return(0);
}
Esempio n. 7
0
int main(int argc, char *argv[]){
	Image* src;
	int offset = 100;
	const int rows = 1000;
  	const int cols = 1000;
  	float r1, r2, r3, r4, r5, r6, r7, r8, r9;
  	int i;
	Color RED;
	Color GREEN;
	Color BLUE;
	Color WHITE;
	Color c1;
	Color c2;
	Color c3;
	Polygon* poly;
	Point points[3];
	char filename[100];
	
	Color_set(&RED, 1.0, 0.0, 0.0);
	Color_set(&GREEN, 0.0, 1.0, 0.0);
	Color_set(&BLUE, 0.0, 0.0, 1.0);
	Color_set(&WHITE, 1.0,1.0,1.0);

	for(i=0; i<offset+1; i++){
		src = image_create(rows, cols);

		//drawing the first triangle
		printf("Preparing and drawing triangle 0\n");
		point_set2D(&points[0], 300, (100 - offset + i));
		point_set2D(&points[1], 700, (100 - offset + i));
		point_set2D(&points[2], 500, (500 - offset + i));

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);


		//drawing the second triangle
		printf("Preparing and drawing triangle 1\n");
		point_set2D(&points[0], 700+offset-i, 100-(offset-i)/2);
		point_set2D(&points[1], 900+offset-i, 500-(offset-i)/2);
		point_set2D(&points[2], 500+offset-i, 500-(offset-i)/2);
		

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);


		//drawing the third triangle
		printf("Preparing and drawing triangle 2\n");
		point_set2D(&points[0], 900+offset-i, 500+(offset-i)/2);
		point_set2D(&points[1], 700+offset-i, 900+(offset-i)/2);
		point_set2D(&points[2], 500+offset-i, 500+(offset-i)/2);

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);


		//drawing the forth triangle
		printf("Preparing and drawing triangle 3\n");
		point_set2D(&points[0], 300, 900 + offset - i);
		point_set2D(&points[1], 700, 900 + offset - i);
		point_set2D(&points[2], 500, 497 + offset - i);

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);


		//drawing the fifth triangle
		printf("Preparing and drawing triangle 4\n");
		point_set2D(&points[0], 100-offset+i, 500+(offset-i)/2);
		point_set2D(&points[1], 300-offset+i, 900+(offset-i)/2);
		point_set2D(&points[2], 500-offset+i, 500+(offset-i)/2);

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);



	//drawing the sixth triangle
		printf("Preparing and drawing triangle 5\n");
		point_set2D(&points[0], 100-offset+i, 500-(offset-i)/2);
		point_set2D(&points[1], 301-offset+i, 100-(offset-i)/2);
		point_set2D(&points[2], 503-offset+i, 500-(offset-i)/2);

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);

		if(i%5==0){
			sprintf(filename, "../images/task6_2_%3.0d.ppm",i);
			image_write(src, filename);
		}
		image_dealloc(src);

	}	
	system("convert -delay 10 -loop 0   ../images/task6_2_*.ppm   ../images/animation.gif");
	system("rm -f ../images/task6_2*");

	//clean up

	polygon_free(poly);
  	image_free(src);


	return 0;
}
Esempio n. 8
0
int main(int argc, char *argv[]){
  Image *src;
  Ellipse elip;
  Circle circ;
  Color color, red, pink, yellow;
  Color blue; 
  Line l;
  Point p;
  Point pt[5];
  Polygon *poly;
  int i;

  /*set the colors*/
  color_set(&color, 1.0, 1.0, 1.0);
  color_set(&red, 1.0, 0.0,0.0);
  color_set(&blue, 0.4,1.0,1.0);
  color_set(&pink, 1.0, 0.6, 0.8);
  color_set(&yellow, 1.0, 1.0, 0.4);

  /*build an image*/
  src = image_create( 700,700); 

  /*build the cupcake like I was using turtle graphics*/
  line_set2D(&l, 100,300,200,600);
  line_draw( &l, src, pink );

  line_set2D(&l, 600,300,500,600);
  line_draw( &l, src, pink );

  point_set2D( &p, 600, 350 );
  ellipse_set(&elip, p, 40, 150,0);
  ellipse_draw(&elip, src, pink);

  point_set2D( &p, 300,350 );
  ellipse_set(&elip, p, 80, 250,0);
  ellipse_draw(&elip, src, pink);

  point_set2D( &p, 275,350 );
  ellipse_set(&elip, p, 75, 225,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 250,350 );
  ellipse_set(&elip, p, 50, 200,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 225,350 );
  ellipse_set(&elip, p, 25, 175,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 200,350 );
  ellipse_set(&elip, p, 20, 150,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 175,350 );
  ellipse_set(&elip, p, 15, 125,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 150,350 );
  ellipse_set(&elip, p, 5, 75,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 350, 100 );
  circle_set( &circ, p, 25 );
  circle_draw( &circ, src, red);

  /*write image*/
  image_write( src, "3Dimage.ppm" );

  /*free image*/
  image_free( src );

  /*this time fill the cupcake*/

  src = image_create( 700,700); 

  color_set(&pink, 1.0, 0.6, 0.8);
  point_set2D( &p, 600, 350 );
  ellipse_set(&elip, p, 40, 150,0);
  ellipse_drawFill(&elip, src, pink);
  
  point_set2D(&(pt[0]),100 ,300);
  point_set2D(&(pt[1]),600 ,300);
  point_set2D(&(pt[2]),505 ,600);
  point_set2D(&(pt[3]),198, 600);
  
  poly = polygon_createp(4, pt);
  polygon_drawFill(poly,src, pink);

  color_set(&pink, 1.0, 0.4, 0.8);
  point_set2D( &p, 300,350 );
  ellipse_set(&elip, p, 80, 250,0);
  ellipse_drawFill(&elip, src, pink);

  color_set(&pink, 1.0, 0., 0.8);
  for(i=0; i<10; i++){
    line_set2D(&l, 100-i,300-i,200,600);
    line_draw( &l, src, pink );

    line_set2D(&l, 600-i,300-i,500,600);
    line_draw( &l, src, pink );

    line_set2D(&l, 175-i, 300-i, 225, 623);
    line_draw(&l, src, pink);

    line_set2D(&l, 275-i, 280-i, 305, 636);
    line_draw(&l, src, pink);

    line_set2D(&l, 385-i, 280-i, 385, 640);
    line_draw(&l, src, pink);

    line_set2D(&l, 510-i, 300-i, 450, 630);
    line_draw(&l, src, pink);

  }


  point_set2D( &p, 295,350 );
  ellipse_set(&elip, p, 70, 230,0);
  ellipse_drawFill(&elip, src, yellow);

  color_set(&blue, 0.0,0.8,0.8);

  point_set2D( &p, 275,350 );
  ellipse_set(&elip, p, 75, 220,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.0,1.0,1.0);

  point_set2D( &p, 250,350 );
  ellipse_set(&elip, p, 50, 200,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.4,1.0,1.0);

  point_set2D( &p, 225,350 );
  ellipse_set(&elip, p, 25, 175,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.6,1.0,1.0);

  point_set2D( &p, 200,350 );
  ellipse_set(&elip, p, 20, 150,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.7,1.0,1.0);

  point_set2D( &p, 175,350 );
  ellipse_set(&elip, p, 15, 125,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.8,1.0,1.0);

  point_set2D( &p, 150,350 );
  ellipse_set(&elip, p, 5, 75,0);
  ellipse_drawFill(&elip, src, blue);

  point_set2D( &p, 350, 100 );
  circle_set( &circ, p, 25 );
  circle_drawFill( &circ, src, red);

  image_write( src, "3DimageFill.ppm" );

  polygon_free(poly);
  image_free( src );

  return(0);

}
Esempio n. 9
0
/*
  Program to test polygon functionality using barycentric coordinates
*/
int main(int argc, char *argv[]) {
  Image *src;
  const int rows = 100;
  const int cols = 100;
  Polygon *p;
  Color Red;
  Color White;
  Color Blue;
  Point pt[100];

  color_set(&Red, 0.9, 0.2, 0.1 );
  color_set(&White, 1.0, 1.0, 1.0 );
  color_set(&Blue, 0.2, 0.1, 0.95 );
  
  src = image_create(rows, cols);

  // make a simple square to test proper areas and locations
  // the square ought to be 20x20, include pixel (30,30) and exclude pixel (50, 50)
  point_set2D(&(pt[0]), 30, 30);
  point_set2D(&(pt[1]), 50, 30);
  point_set2D(&(pt[2]), 50, 50);
  point_set2D(&(pt[3]), 30, 50);
  point_set2D(&(pt[4]), 30, 30);

  p = polygon_createp(3, pt);

  printf("drawing polygon 1\n");
  polygon_drawFillB(p, src, Blue);

  polygon_set(p, 3, &(pt[2]) );

  printf("drawing polygon 2\n");
  polygon_drawFillB(p, src, Red);

  point_set2D(&(pt[5]), 60, 20);
  point_set2D(&(pt[6]), 80, 85);
  point_set2D(&(pt[7]), 50, 70);
  polygon_set(p, 3, &(pt[5]));

  printf("drawing polygon 3\n");
  polygon_drawFillB(p, src, White);

  point_set2D(&(pt[8]), 5, 5);
  point_set2D(&(pt[9]), 25, 5);
  point_set2D(&(pt[10]), 25, 25);
  point_set2D(&(pt[11]), 5, 25);
  point_set2D(&(pt[12]), 5, 5);

  polygon_set(p, 3, &(pt[10]) );

  printf("drawing polygon 4\n");
  polygon_drawFillB(p, src, Red);

  polygon_set(p, 3, &(pt[8]));

  printf("drawing polygon 5\n");
  polygon_drawFillB(p, src, Blue);


  printf("writing output\n");
  image_write(src, "test4b.ppm");

  image_free(src);

  return(0);
}
Esempio n. 10
0
/*
  Program to test polygon functionality
*/
int main(int argc, char *argv[]) {
  Image *src;
  const int rows = 300;
  const int cols = 400;
  Polygon *p;
  Color Red;
  Color Orange;
  Color White;
  Color Blue;
  Point pt[100];
  int i;
  
  srand(42);

  color_set(&Red, 0.9, 0.2, 0.1 );
  color_set(&Orange, 0.95, 0.7, 0.3 );
  color_set(&White, 1.0, 1.0, 1.0 );
  color_set(&Blue, 0.2, 0.1, 0.95 );

  src = image_create(rows, cols);

  // make a simple square to test proper areas and locations
  // the square ought to be 20x20, include pixel (30,30) and exclude pixel (50, 50)
  point_set2D(&(pt[0]), 30, 30);
  point_set2D(&(pt[1]), 50, 30);
  point_set2D(&(pt[2]), 50, 50);
  point_set2D(&(pt[3]), 30, 50);

  p = polygon_createp(4, pt);

  printf("drawing a square\n");
  polygon_drawFill(p, src, Blue);

  // something more interesting
  for(i=0;i<50;i++) {
    float dr = rand() % 20;
    point_set2D(&(pt[i]), 
    200 + cos((float)i * M_PI * 2.0 / 50.0)*(70 + dr),
    150 + sin((float)i * M_PI * 2.0 / 50.0)*(70 + dr));
  }
  polygon_set(p, 50, pt);

  printf("drawing first big polygon\n");
  polygon_drawFill(p, src, Red);

  for(i=0;i<50;i++) {
    float dr = rand() % 15;
    point_set2D(&(pt[i]), 
    200 + cos((float)i * M_PI * 2.0 / 50.0)*(50 + dr),
    150 + sin((float)i * M_PI * 2.0 / 50.0)*(50 + dr));
  }
  polygon_set(p, 50, pt);

  printf("drawing second big polygon\n");
  polygon_drawFill(p, src, Orange);

  for(i=0;i<50;i++) {
    float dr = rand() % 10;
    point_set2D(&(pt[i]), 
    200 + cos((float)i * M_PI * 2.0 / 50.0)*(30 + dr),
    150 + sin((float)i * M_PI * 2.0 / 50.0)*(30 + dr));
  }
  polygon_set(p, 50, pt);

  printf("drawing third big polygon\n");
  polygon_drawFill(p, src, White);

  printf("writing output\n");
  image_write(src, "test4a.ppm");

  image_free(src);

  return(0);
}