示例#1
0
文件: main.c 项目: stuydw/line
int main() {

  screen s;
  color c;
 
  
  c.red = MAX_COLOR;
  c.green = MAX_COLOR;
  c.blue = 0;
  
  clear_screen(s);

  int i, j;

  for (i=0; i < YRES; i++)
    for (j=0; j < XRES; j++)
      plot(s, c, i, j);
  
  c.green = 0;
  for (i = 0; i<100; i++) {
    draw_line(250, 250, 10+i, 10+i, s, c);
  }
  draw_line(1, 1, 60, 70, s, c);
  //Note: Display may not work on your system
  //save_ppm and save_extension should be fine
  display(s);
  save_ppm(s, "pic.ppm");
  save_extension(s, "whatevs.png");
}
示例#2
0
文件: main.c 项目: stuydw/line
int main() {

  screen s;
  color c;
 
  
  c.red = MAX_COLOR;
  c.green = MAX_COLOR;
  c.blue = 0;
  
  clear_screen(s);

  int i, j;

  for (i=0; i < YRES; i++)
    for (j=0; j < XRES; j++ )
      plot(s, c, i, j);
  
  c.green = 0;
  
  draw_line(20,20,200,500,s,c); //slope > 1
  draw_line(0,0,500,300,s,c); // 0 < slope < 1
  draw_line(0,350,350,200,s,c); // -1 < slope < 0
  draw_line(0,350,300,0,s,c); // slope < -1

  //Note: Display may not work on your system
  //save_ppm and save_extension should be fine
  display(s);
  save_ppm(s, "pic.ppm");
  save_extension(s, "whatevs.png");
}  
示例#3
0
/*======== void display() ==========
Inputs:   screen s
Returns:
Will display the screen s on your monitor

02/12/10 09:16:30
jdyrlandweaver
====================*/
void display( screen s) {
   int x, i;
  char *fname = ".tmp.png";
  save_extension(s, fname);
  i = fork();
  if (i == 0) {
    execlp("display", "display", fname, NULL);
  }
  else {
    wait(&x);
    remove( fname );
  }
  /* For some reason, this refuses to run correctly
     on some systems. Most likely a strange imagemagick
     install issue.
     Above is a workaroudn for now.
  int x, y;
  FILE *f;

  f = popen("display", "w");

  fprintf(f, "P3\n%d %d\n%d\n", XRES, YRES, MAX_COLOR);
  for ( y=0; y < YRES; y++ ) {
    for ( x=0; x < XRES; x++)

      fprintf(f, "%d %d %d ", s[x][y].red, s[x][y].green, s[x][y].blue);
    fprintf(f, "\n");
  }
  pclose(f);
  */
}
示例#4
0
文件: main.c 项目: sduncan/Code
int main() {

  screen s;
  color c;
 
  
  c.red = MAX_COLOR;
  c.green = MAX_COLOR;
  c.blue = 0;
  
  clear_screen(s);

  int i, j;

  for (i=0; i < YRES; i++)
    for (j=-500; j < XRES; j++ )
      plot(s, c, i, j);
  
  c.green = 0;

  draw_line(0, 0, 400, 350, s, c); //oct 1
  draw_line(0, 0, 400, 450, s, c); //oct 2
  //draw_line(20, 20, 300, 250, s, c); //oct 5
  draw_line(20, 350, 300, 100, s, c); //oct 6
  
  //draw_line(0, 0, 400, 350, s, c);
  //draw_line(0, 0, 400, 350, s, c);
  
  //Note: Display may not work on your system
  //save_ppm and save_extension should be fine
  save_ppm(s, "pic.ppm");
  save_extension(s, "whatevs.png");
  //display(s);
}  
示例#5
0
文件: main.c 项目: stuydw/line
int main() {

  screen s;
  color c;
 
  
  c.red = MAX_COLOR;
  c.green = MAX_COLOR;
  c.blue = 0;
  
  clear_screen(s);

  int i, j;

  for (i=0; i < YRES; i++)
    for (j=0; j < XRES; j++ )
      plot(s, c, i, j);
  
  c.green = 0;

  int counter = 0;
  for(counter ; counter < 100 ; counter++) {
    draw_line((250 - counter), (250 - (counter * 2)), (150 + (3 * counter)),
    (counter * 5), s, c);
  }

  //Note: Display may not work on your system
  //save_ppm and save_extension should be fine
  display(s);
  save_ppm(s, "pic.ppm");
  save_extension(s, "whatevs.png");
}
示例#6
0
文件: main.c 项目: stuydw/matrix
int main() {

  screen s;
  color c;

  c.red = 0;
  c.green = 255;
  c.blue = 255;

  int i, j;

  for( i=0; i<XRES; i++) 
    for ( j=0; j<YRES; j++) {
      /*
      c.red = random() % (MAX_COLOR + 1);
      c.green = random() % (MAX_COLOR + 1);
      c.blue = random() % (MAX_COLOR + 1);
      */
      c.red= 190; 
      c.blue =150; 
      c.green=175; 
      plot( s, c, i, j);
    }

  c.blue=255; 
  c.red =0; 
  c.green = 150; 

  printf("Matrix Testing Area. Please stand well out of range of the matrix that is about to happen.\n"); 

  printf("Making a 3X6 matrix.\n\n"); 
  struct matrix* testing = new_matrix(3,6); 
  add_edge(testing, 1, 11, 1, 2, 12, 1); 
  add_edge(testing, 3, 13, 1, 4, 14, 1); 
  add_edge(testing, 5, 15, 1, 6, 16, 1); 

  printf("Points made. Print test.\n"); 
  print_matrix(testing); 
  printf("Scalar Multiplication Test:\n"); 
  scalar_mult(3, testing); 
  print_matrix(testing); 

  printf("Identity test. Time to face the facts.\n"); 
  struct matrix* identity = new_matrix(6, 6); 
  ident(identity); 
  print_matrix(identity); 

  printf("Testing matrix multiplication. Prepare your eyes. Multiplying by the identity matrix now.\n"); 
  matrix_mult(testing, identity); 
  print_matrix(identity); 


  drawPicture(s, c); 
  display( s );    
  save_ppm(s,  "image" );
  save_extension(s, "image.jpg");
  
}  
示例#7
0
文件: main.c 项目: young-k/Notes
int main() {

  screen s;
  color c;

  /* Setup color and screen */

  clear_screen(s);
  
  c.red = 255;
  c.green = 150;
  c.blue = 100;

  /* Setup matrices */
  
  struct matrix *edges;
  struct matrix *transform;

  edges = new_matrix(4, 1);

  /* Finally Testing */
  
  add_edge(edges, 80, 80, 0, 80, 120, 0);
  add_edge(edges, 80, 120, 0, 120, 120, 0);
  add_edge(edges, 120, 120, 0, 120, 80, 0);
  add_edge(edges, 120, 80, 0, 80, 80, 0);

  add_edge(edges, 60, 80, 0, 80, 120, 0);
  add_edge(edges, 80, 120, 0, 100, 100, 0);
  add_edge(edges, 100, 100, 0, 80, 60, 0);
  add_edge(edges, 80, 60, 0, 60, 80, 0);

  add_edge(edges, 90, 90, 0, 110, 90, 0);
  add_edge(edges, 110, 90, 0, 110, 110, 0);
  add_edge(edges, 110, 110, 0, 90, 110, 0);
  add_edge(edges, 90, 110, 0, 90, 90, 0);
  
  draw_lines(edges, s, c);

  int i;
  for (i = 0; i < 40; i+=2) {
    transform = make_translate(i, i, i);
    matrix_mult(transform, edges);
    draw_lines(edges, s, c);
  }  
  
  display(s);
  save_extension(s, "matrix.png");

  /* Free Matrices */
  
  free_matrix( transform );
  free_matrix( edges );

  return 0;
}  
示例#8
0
文件: main.c 项目: stuydw/line
int main() {

    screen s;
    color c;

    int x1, y1;

    c.red = MAX_COLOR;
    c.green = MAX_COLOR;
    c.blue = MAX_COLOR;

    clear_screen(s);

    int i, j;

    for (i=0; i < YRES; i++)
        for (j=0; j < XRES; j++ )
            plot(s, c, i, j);

    c.green = 0;
    c.blue = 0;
    
    x1 = 500;
    y1 = 250;
    while (x1 >= 250) {
        draw_line(250, 250, x1, y1, s, c);
        c.red-=2;
        c.green++;
        x1--;
        y1++;
    }
    while (y1 >= 250) {
        draw_line(250, 250, x1, y1, s, c);
        c.green--;
        c.blue--;
        x1--;
        y1--;
    }
    while (x1 <= 250) {
        draw_line(250, 250, x1, y1, s, c);
        c.red++;
        x1++;
        y1--;
    }
    while (y1 <= 250) {
        draw_line(250, 250, x1, y1, s, c);
        c.green++;
        x1++;
        y1++;
    }
    //Note: Display may not work on your system
    //save_ppm and save_extension should be fine
    display(s);
    save_ppm(s, "pic.ppm");
    save_extension(s, "whatevs.png");
}  
示例#9
0
int main() {

  screen s;
  color c;
 
  
  c.red = 0;
  c.green = MAX_COLOR;
  c.blue = 0;
  
  clear_screen(s);


  //octant 1
  draw_line( 0, 0, XRES-1, YRES - 75, s, c);
  //octant 2
  draw_line( 0, 0, XRES - 75, YRES-1, s, c); 
  //octant 8
  draw_line( 0, YRES-1, XRES-1, 75, s, c);  
  //octant 7
  draw_line( 0, YRES-1, XRES - 75, 0, s, c);
  
  c.green = 0;
  c.blue = MAX_COLOR;
  //octant 5
  draw_line( XRES - 1, YRES - 1, 0, 75, s, c);
  //octant 6
  draw_line( XRES - 1, YRES -1, 75, 0, s, c);
  //octant 4
  draw_line( XRES - 1, 0, 0, YRES - 75, s, c);
  //octant 3
  draw_line( XRES - 1, 0, 75, YRES - 1, s, c);
  
  c.blue = 0;
  c.red = MAX_COLOR;
  //y = x, y = -x
  draw_line( 0, 0, XRES - 1, YRES - 1, s, c);
  draw_line( 0, YRES - 1, XRES - 1, 0, s, c);

  //horizontal, vertical line
  draw_line( 0, YRES / 2, XRES - 1, YRES / 2, s, c);
  draw_line( XRES / 2, 0, XRES / 2, YRES - 1, s, c);
  
  int len = 1;
  int i;
  while (i<XRES-1){
    c.blue = (c.blue + 1)%256;
    draw_line(i,i,(i*i)%XRES,i,s,c);
    draw_line(i,i,i,(i*i)%YRES,s,c);
    i++;
  }

  display(s);
  save_extension(s, "lines.png");
}  
示例#10
0
文件: main.c 项目: stuydw/line
int main() {

  screen s;
  color c;
 
  
  c.red = MAX_COLOR;
  c.green = MAX_COLOR;
  c.blue = 200;
  
  clear_screen(s);

  int i, j;

  for (i=0; i < YRES; i++)
    for (j=0; j < XRES; j++ )
      plot(s, c, i, j);
  
  c.green = 0;
 
  //basic test
  /*
  draw_line(250,250,250, 500, s, c); 
  draw_line(250,250,250, 0, s, c); 
  draw_line(250,250,500, 250, s, c); 
  draw_line(250,250,0, 250, s, c); 
  draw_line(250,250,500, 500, s, c); 
  draw_line(250,250, 0, 500, s, c); 
  draw_line(250,250,500, 0, s, c); 
  draw_line(250,250, 0, 0, s, c);*/
  
  int y = 500; 
  int x = 250;
  int counter = 0; 
  srand(time(NULL)); 
  int r = rand() % 501; 
 
  while (counter != 100){

    draw_line(x, y, r, r, s, c); 
    r = rand () %501; 
    x = rand() % 501; 
    y = rand()%501; 
  
    counter++; 
  }


  //Note: Display may not work on your system
  //save_ppm and save_extension should be fine
  display(s);
  save_ppm(s, "pic.ppm");
  save_extension(s, "whatevs.png");
}  
示例#11
0
文件: main.c 项目: stuydw/line
int main() {

  screen s;
  color c;
 
  
  c.red = 0;
  c.green = 0;
  c.blue = 0;
  
  clear_screen(s);

  int i,j;

  for (i=0; i < YRES; i++)
    for (j=0; j < XRES; j++ )
      plot(s, c, i, j);
  
  for (i=0; i< XRES; i++) {
    c.red = i % 256;
    c.green = 256 - (i % 256);
    c.blue = 0;
    draw_line(256, 256, i, 512, s, c);
  }

  for (i=0; i< XRES; i++) {
    c.red = 256 - (i % 256);
    c.green = i % 256;
    c.blue = 64;
    draw_line(256, 256, i, 0, s, c);
  }

  for (i=0; i< XRES; i++) {
    c.red = 0;
    c.green = 256 - (i % 256);
    c.blue = i % 256;
    draw_line(256, 256, 0, i, s, c);
  }

  for (i=0; i< XRES; i++) {
    c.red = i % 256;
    c.green = 0;
    c.blue = 256 - (i % 256);
    draw_line(256, 256, 512, i, s, c);
  }

  //Note: Display may not work on your system
  //save_ppm and save_extension should be fine
  display(s);
  save_ppm(s, "pic.ppm");
  save_extension(s, "whatevs.png");
}  
示例#12
0
文件: main.c 项目: stuydw/line
int main() {

  screen s;
  color c;
 
  c.red = 0;
  c.green = 0;
  c.blue = 0;
  
  clear_screen(s);

  int i, j;

  for (i=0; i < YRES; i++)
    for (j=0; j < XRES; j++ )
      plot(s, c, i, j);
  
  c.red = MAX_COLOR;
  //S
  draw_line(75, 300, 125, 300, s, c); 
  draw_line(75, 300, 75, 250, s, c); 
  draw_line(75, 250, 125, 250, s, c); 
  draw_line(125, 250, 125, 200, s, c); 
  draw_line(125, 200, 75, 200, s, c); 
  
  //O
  draw_line(150, 300, 200, 300, s, c);
  draw_line(150, 200, 200, 200, s, c);
  draw_line(150, 300, 150, 200, s, c);
  draw_line(200, 300, 200, 200, s, c);
  
  //N
  draw_line(225, 300, 225, 200, s, c);
  draw_line(275, 300, 275, 200, s, c);
  draw_line(225, 300, 275, 200, s, c);
  //E
  draw_line(300, 300, 300, 200, s, c);
  draw_line(300, 300, 350, 300, s, c);
  draw_line(300, 250, 325, 250, s, c);
  draw_line(300, 200, 350, 200, s, c);
  //Note: Display may not work on your system
  //save_ppm and save_extension should be fine
  display(s);
  save_ppm(s, "pic.ppm");
  save_extension(s, "whatevs.png");
} 
示例#13
0
int main(){

  screen s;
  color c;

  c.red = 0;
  c.green = 0;
  c.blue = MAX_COLOR;

  int ctr = 0;
  while (ctr <= 200){
    draw_line(0, ctr, XRES-1, YRES-1, s, c);
    ctr ++;
    c.red = ctr % MAX_COLOR;
  }

  c.red = 0;
  ctr = 0;
  while (ctr <= 200){
    draw_line(XRES-1, YRES-1, ctr, 0, s, c);
    ctr ++;
    c.green = ctr % MAX_COLOR;
  }

  c.green = 0;
  ctr = 0;
  while (ctr < XRES){
    draw_line(0, 201, ctr, YRES-1, s, c);
    ctr ++;
    c.green = ctr % MAX_COLOR;
  }

  c.green = 0;
  ctr = 0;
  while (ctr < YRES){
    draw_line(201, 0, XRES-1, ctr, s, c);
    ctr ++;
    c.red = ctr % MAX_COLOR;
  }
  
  
  save_extension(s, "pic.png");
  display(s);
  
  return 0;
}
示例#14
0
文件: main.c 项目: JayStuy/line_maker
int main() {

  screen s;
  color c;
 
  
  c.red = 0;
  c.green = MAX_COLOR;
  c.blue = 0;
  
  clear_screen(s);


  //octant 1
  draw_line( 0, 0, XRES-1, YRES - 75, s, c);
  //octant 2
  draw_line( 0, 0, XRES - 75, YRES-1, s, c); 
  //octant 8
  draw_line( 0, YRES-1, XRES-1, 75, s, c);  
  //octant 7
  draw_line( 0, YRES-1, XRES - 75, 0, s, c);

  c.green = 0;
  c.blue = MAX_COLOR;
  //octant 5
  draw_line( XRES - 1, YRES - 1, 0, 75, s, c);
  //octant 6
  draw_line( XRES - 1, YRES -1, 75, 0, s, c);
  //octant 4
  draw_line( XRES - 1, 0, 0, YRES - 75, s, c);
  //octant 3
  draw_line( XRES - 1, 0, 75, YRES - 1, s, c);
  
  c.blue = 0;
  c.red = MAX_COLOR;
  //y = x, y = -x
  draw_line( 0, 0, XRES - 1, YRES - 1, s, c);
  draw_line( 0, YRES - 1, XRES - 1, 0, s, c);

  //horizontal, vertical line
  draw_line( 0, YRES / 2, XRES - 1, YRES / 2, s, c);
  draw_line( XRES / 2, 0, XRES / 2, YRES - 1, s, c);
  */
  display(s);
  save_extension(s, "lines.png");
}  
示例#15
0
文件: main.c 项目: stuydw/line
int main() {

  screen s;
  color c;
 
  
  c.red = 0;
  c.green = 0;
  c.blue = 0;
  
  clear_screen(s);

  int i, j;

  for (i=0; i < YRES; i++)
    for (j=0; j < XRES; j++ )
      plot(s, c, i, j);
  
  c.green = MAX_COLOR;
  c.blue = MAX_COLOR;
  c.red = MAX_COLOR;

  //too lazy to finish the 2 of hearts

  draw_line(90, 1, 410, 1, s, c);
  draw_line(90, 499, 410, 499, s, c);
  draw_line(90, 1, 90, 499, s, c);
  draw_line(410, 1, 410, 499, s, c);
  draw_line(110, 480, 140, 480, s, c);
  draw_line(140, 480, 140, 460, s, c);
  draw_line(140, 460, 110, 445, s, c);
  draw_line(110, 445, 110, 425, s, c);
  draw_line(110, 425, 140, 425, s, c);

  draw_line(360, 20, 390, 20, s, c);
  draw_line(360, 20, 360, 40, s, c);
  draw_line(360, 40, 390, 55, s, c);
  draw_line(390, 55, 390, 75, s, c);
  draw_line(360, 75, 390, 75, s, c);

  //Note: Display may not work on your system
  //save_ppm and save_extension should be fine
  display(s);
  save_ppm(s, "pic.ppm");
  save_extension(s, "whatevs.png");
}  
示例#16
0
int main( int argc, char** argv ) {

  screen s;
  struct matrix *edges;
  struct matrix *transform;
  color c;
  c.red = 0;
  c.green = 255;
  c.blue = 255;

  edges = new_matrix(4, 4);
  transform = new_matrix(4, 4);

  if ( argc == 2 )
    parse_file( argv[1], transform, edges, s );
  else
    parse_file( "stdin", transform, edges, s );

  add_edge( edges, 250,0,0, 250,25,0 );//M                       
  add_edge( edges, 250,25,0, 263,0,0 );
  add_edge( edges, 263,0,0, 275,25,0 );
  add_edge( edges, 275,25,0, 275,0,0 );
  add_edge( edges, 280,0,0, 293,25,0 );//A                      
  add_edge( edges, 293,25,0, 305,0,0 );
  add_edge( edges, 287,13,0, 299,13,0 );
  add_edge( edges, 310,0,0, 325,25,0 );//Y                               
  add_edge( edges, 318,13,0, 305,25,0 );
  add_edge( edges, 330,0,0, 343,25,0 );//A                              
  add_edge( edges, 343,25,0, 355,0,0 );
  add_edge( edges, 337,13,0, 349,13,0 );
  add_edge( edges, 360,0,0, 360,25,0 );//N                         
  add_edge( edges, 360,25,0, 385,0,0 );
  add_edge( edges, 385,0,0, 385,25,0 );
  add_edge( edges, 390,0,0, 390,25,0 );//K                           
  add_edge( edges, 390,13,0, 408,25,0 );
  add_edge( edges, 395,14,0, 408,0,0 );
  draw_lines(edges, s, c);

  save_extension(s, "dimensional.png");
  display(s);

  free_matrix( transform );
  free_matrix( edges );
}  
示例#17
0
文件: main.c 项目: young-k/Notes
int main() {

  screen s;
  color c;
 
  
  c.red = 255;
  c.green = MAX_COLOR;
  c.blue = 180;

  clear_screen(s);
  display(s);

  int i;

  for(i = 0; i < YRES; i+=2) {
    draw_line( XRES / 2, YRES / 2, XRES, i, s, c);
    c.green = (int)((255.0 * i) / YRES);
  }

  for(i = 0; i < YRES; i+=2) {
    draw_line( XRES / 2, YRES / 2, 0, i, s, c);
    c.green = (int)(255.0 * i / YRES);
  }
  
  for(i = 0; i < XRES; i+=2) {
    draw_line( XRES / 2, YRES / 2, i, 0, s, c);
    c.green = (int)(255.0 * i / XRES);
  }

  for(i = 0; i < XRES; i+=2) {
    draw_line( XRES / 2, YRES / 2, i, YRES, s, c);
    c.green = (int)(255.0 * i / XRES);
  }
  
  display(s);
  save_extension(s, "lines.png");
}  
示例#18
0
文件: main.c 项目: stuydw/matrix
int main() {

    screen s;
    color c;

    c.red = 0;
    c.green = 255;
    c.blue = 255;

    int i, j;

    for( i=0; i<XRES; i++)
        for ( j=0; j<YRES; j++) {

            c.red = random() % (MAX_COLOR + 1);
            c.green = random() % (MAX_COLOR + 1);
            c.blue = random() % (MAX_COLOR + 1);

            plot( s, c, i, j);
        }

    struct matrix * points = new_matrix(4, 4);
    add_point(points, 50, 250, 0);
    add_point(points, 70, 250, 0);
    add_edge(points, 30, 100, 0, 300, 400, 0);
    draw_lines(points, s, c);
    print_matrix(points);
    printf("\n");
    scalar_mult(2.0, points);
    printf("\n");
    ident(points);
    printf("\n");
    display( s );
    save_ppm(s,  "picture" );
    save_extension(s, "picture.jpg");

}
示例#19
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
      takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,bv    
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {


  double args [6];
  char command [25];

  color c;
  c.red = 255;
  c.blue = 0;
  c.green = 0;

  FILE *f = fopen(filename, "r");


  if(f == NULL) {
    printf("cannot find file\n");
    exit(1);
  }

  while(fgets(command, 25, f)){
    if(command[0] == 'l') {
      fgets(command, 25, f);
      sscanf(command, "%lf %lf %lf %lf %lf %lf", &args[0], &args[1], &args[2], &args[3], &args[4], &args[5]);
      add_edge(pm, args[0], args[1], args[2], args[3], args[4], args[5]);
    }

    else if (command[0] == 'i') {
      ident(transform);
    }

    else if (command[0] == 's') {
      fgets(command, 25, f);
      sscanf(command, "%lf %lf %lf", &args[0], &args[1], &args[2]);
      matrix_mult(make_scale(args[0], args[1], args[2]), transform);
    }

    else if (command[0] == 't') {
      fgets(command, 25, f);
      sscanf(command, "%lf %lf %lf", &args[0], &args[1], &args[2]);
      struct matrix *m;
      m = make_translate(args[0], args[1], args[2]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'x') {
      fgets(command, 25, f);
      sscanf(command, "%lf", &args[0]);
      struct matrix *m;
      m = make_rotX(args[0]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'y') {
      fgets(command, 25, f);
      sscanf(command, "%lf", &args[0]);
      struct matrix *m;
      m = make_rotY(args[0]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'z') {
      fgets(command, 25, f);
      sscanf(command, "%lf", &args[0]);
      struct matrix *m;
      m = make_rotZ(args[0]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'a') {
      matrix_mult(transform, pm);
    }

    else if (command[0] == 'v') {
      draw_lines(pm, s, c);
    }

    else if (command[0] == 'g') {
      draw_lines(pm, s, c);
      fgets(command, 25, f);
      int i = strlen(command);
      command[i] = '\0';
      save_extension(s, command);
    }

    else if (command[0] == 'q') {
      exit(1);
    }
  }
  
}
示例#20
0
文件: parser.c 项目: nspektor/3dHW
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  g.red = 255;
  g.green = 0;
  g.blue = 255;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
    double width, height, depth, radius, radius1, radius2;
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      //printf("BEZIER\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
    }
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      //printf("BOX\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf",
	     &x, &y, &z, &width, &height, &depth);
      add_box(pm, x, y, z, width, height, depth);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "sphere", strlen(line)) == 0 ) {
      //printf("SPHERE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &radius);
      add_sphere(pm, x, y, radius, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "torus", strlen(line)) == 0 ) {
      //printf("TORUS\n");ds
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &radius1, &radius2 );
      add_torus(pm, x, y, radius1, radius2, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      //      line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      clear_screen(s);
      draw_lines(pm, s, g);
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
      draw_lines(pm, s, g);
      save_extension(s, line);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
    }
    
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    else if (strncmp(line, "#", strlen(1)) == 0){
    }
    else {
      printf("Invalid command\n");
    }
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
示例#21
0
文件: parser.c 项目: RongYu98/MDL
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         sphere: add a sphere to the edge matrix - 
	    takes 3 arguemnts (cx, cy, r)
         torus: add a torus to the edge matrix - 
	    takes 4 arguemnts (cx, cy, r1, r2)
         box: add a rectangular prism to the edge matrix - 
	    takes 6 arguemnts (x, y, z, width, height, depth)
	 clear: clear the currnt edge matrix -
	    takes 0 arguments
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  struct stack * STACK = new_stack();
  
  g.red = 0;
  g.green = 255;
  g.blue = 0;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
   
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;
    }    
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;

    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;
    }
    
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_box(pm, x, y, z, x1, y1, z1);
      matrix_mult( STACK->data[ STACK->top ], pm);
      draw_polygons( pm, s, g );
      pm->lastcol = 0;
    }
    
    else if (strncmp(line, "sphere", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 10);
      matrix_mult( STACK->data[ STACK->top ], pm);
      draw_polygons( pm, s, g );
      pm->lastcol = 0;
    }
    else if (strncmp(line, "torus", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
      add_torus(pm, x, y, z, z1, 10);
      matrix_mult( STACK->data[ STACK->top ], pm);
      draw_polygons( pm, s, g );
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      //matrix_mult(tmp, transform);
      //print_matrix(transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f); 
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
    }
    else if ( strncmp(line, "print", strlen(line)) == 0) {
      print_matrix( STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      //clear_screen(s);
      //draw_polygons(pm, s, g);
      save_extension(s, line);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    else if ( strncmp(line, "push", strlen(line)) == 0 ) {
      push( STACK ); //seg fault
      printf("Pushed\n");
    }
    else if ( strncmp(line, "pop", strlen(line)) == 0 ) {
      pop( STACK );
      printf("Popped\n");
    } 
    else if ( line[0] != '#' ) {
      printf("Invalid command\n");
    }
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
示例#22
0
int main() {

  screen s;
  color c;
  int i, j; 
  
  c.red = 0;
  c.green = MAX_COLOR;
  c.blue = 0;
  
  clear_screen(s);

  /*
  //octant 1
  draw_line( 0, 0, XRES-1, YRES - 75, s, c);  
  //  draw_line( XRES-1, YRES - 75, 0, 0, s, c);  

  //octant 2
  draw_line( 0, 0, XRES - 75, YRES-1, s, c); 
  //octant 8
  draw_line( 0, YRES-1, XRES-1, 75, s, c);  
  //octant 7
  draw_line( 0, YRES-1, XRES - 75, 0, s, c);

  c.green = 0;
  c.blue = MAX_COLOR;
  //octant 5
  draw_line( XRES - 1, YRES - 1, 0, 75, s, c);
  //octant 6
  draw_line( XRES - 1, YRES -1, 75, 0, s, c);
  //octant 4
  draw_line( XRES - 1, 0, 0, YRES - 75, s, c);
  //octant 3
  draw_line( XRES - 1, 0, 75, YRES - 1, s, c);
  
  c.blue = 0;
  c.red = MAX_COLOR;
  //y = x, y = -x
  draw_line( 0, 0, XRES - 1, YRES - 1, s, c);
  draw_line( 0, YRES - 1, XRES - 1, 0, s, c);

  //horizontal, vertical line
  draw_line( 0, YRES / 2, XRES - 1, YRES / 2, s, c);
  draw_line( XRES / 2, 0, XRES / 2, YRES - 1, s, c);

  draw_coord(s, c);
  draw_border(s, c);
  */
  c.red = MAX_COLOR;
  c.green = 0;
  c.blue = 0;
  for ( i = 0; i < XRES; i+=6 ) {
    draw_line( XRES / 2, YRES / 2, i, 0, s, c );
    c.red = c.red - 3;
    c.green = c.green + 3;
  }

  c.red = 0;
  c.green = MAX_COLOR;
  for ( i = 0; i < YRES; i+=6 ) {
    draw_line( XRES / 2, YRES / 2, YRES, i, s, c );
    c.green = c.green - 3;
    c.blue = c.blue + 3;
  }

  c.green = 0;
  c.blue = MAX_COLOR;
  for (i = 0; i < XRES; i+=6 ) {
    draw_line( XRES / 2, YRES /2, XRES - i, YRES, s, c );
    c.blue = c.blue - 3;
    c.green += 3;
    c.red += 3;
  }

  c.blue = 0;
  for (i = 0; i < XRES; i+=6 ) {
    draw_line( XRES / 2, YRES /2, 0, YRES - i, s, c );
    c.green -= 3;
  }

  for ( i = 0; i < XRES; i++ ) {
    for ( j = 0; j < YRES; j++ ) {
      if ( i % 100 <= 3 || j % 100 <= 3 ) {
	c.red = MAX_COLOR;
	c.green = MAX_COLOR / 2 + 50;
	c.blue = MAX_COLOR;
	plot (s, c, i, j);
      }
    }
  }

  for ( i = 0; i < XRES; i+=10 ) {
    c.red = 0;
    c.green = 0;
    c.blue = 0;
    draw_line( i, 0, 0, i, s, c );
    draw_line( i, 0, YRES - i, XRES, s, c );
    draw_line( 0, i, XRES, YRES - i, s, c );
  }

  draw_border(s, c);

  save_extension(s, "lines.png");  
  display(s);

}  
示例#23
0
文件: parser.c 项目: stuydw/polygons
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
	    takes 6 arguments (x0, y0, z0, x1, y1, z1)
         b: add a hermite curve to the edge matrix - 
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         h: add a bezier to the edge matrix - 
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         c: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
         m: add a sphere to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
         d: add a torus to the edge matrix - 
	    takes 4 arguments (cx, cy, r1, r2)
         p: add a rectangular prism to the edge matrix - 
	    takes 6 arguments (x, y, z, width, height, depth)
	 w: clear the current edge matrix -
	    takes 0 arguments
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

03/08/12 16:22:10
jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  g.red = 0;
  g.green = 255;
  g.blue = 255;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    char c;
    double x, y, z, x1, y1, z1, x2, y2, x3, y3, x4, y4;
   
    c = line[0];

    switch (c) {
    case 'l':
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
      break;
    case 'p':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_box(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
      break;
    case 'm':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 0.05);
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'd':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
      add_torus(pm, x, y, z, z1, 0.05);
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'c':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'b':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'h':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 's':
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
      break;
    case 't':
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      //      line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
      break;
    case 'x':
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      matrix_mult(tmp, transform);
      break;
    case 'y':
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      matrix_mult(tmp, transform);
      break;
    case 'z':
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      matrix_mult(tmp, transform);
      break;
    case 'i':
      ident(transform);
      break;
    case 'a':
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
      break;
    case 'v':
 
      /*
      clear_screen(s);
      draw_lines(pm, s, g);
      display(s);
      break;
      */
      
      // Second version for triangles:
      clear_screen(s);
      draw_polygons(pm, s, g);
      display(s);
      break;
      
    case 'w':
      pm->lastcol = 0;
      break;
    case 'g':
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
      draw_polygons(pm, s, g);
      save_extension(s, line);
      break;
    case 'q':
      return;
    case '#':
      break;
    default:
      printf("Invalid command\n");
      break;
    }
  }

  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
示例#24
0
文件: main.c 项目: stuydw/matrix
int main() {

  //Basic Matrix Math
  struct matrix *a;
  struct matrix *b;

  a=new_matrix(4,4);
  b=new_matrix(4,2);
 
  
  printf("Identity matrix:\n");
  ident(a);
  print_matrix(a);
  
  b->m[0][0]=1;
  b->m[0][1]=2;
  b->m[1][0]=3;
  b->m[1][1]=4;
  b->m[2][0]=5;
  b->m[2][1]=6;
  b->m[3][0]=7;
  b->m[3][1]=8;
  
  printf("Matrix #2:\n");
  print_matrix(b);
  
  printf("Scalar Multiplication by 2:\n");
  scalar_mult(2, b);
  print_matrix(b);
  
  printf("New Matrix #1:\n");
  a->m[2][1]=3;
  a->m[0][3]=2;
  print_matrix(a);
  
  printf("Matrix Multiplication:\n");
  matrix_mult(a, b);
  print_matrix(b);

  printf("Adding points/edges:\n");
  struct matrix *d;
  d = new_matrix(3, 3);
  add_point(d, 200,400,70);
  add_point(d, 200,0,7);
  print_matrix(d);
  printf("\n");
  add_edge(d, 300,500,100,300,100,134);
  add_edge(d, 100,500,100,100,100,134);
  add_edge(d, 400,00,100,400,400,134);
  print_matrix(d);
  printf("\n");


  screen s;
  color c;

  c.red = 200;
  c.green = 100;
  c.blue = 250;

  int i, j;



  for( i=0; i<XRES; i++) 
    for ( j=0; j<YRES; j++) {
      plot( s, c, i, j);
    }

  c.red=0;
  c.green=200;
  c.blue=200;

  draw_lines(d, s, c);
 
  display( s );    
  save_ppm(s,  "image" );
  save_extension(s, "image.jpg");
  
}  
示例#25
0
/*======== void my_main() ==========
Inputs:
Returns:

This is the main engine of the interpreter, it should
handle most of the commadns in mdl.

If frames is not present in the source (and therefore
num_frames is 1, then process_knobs should be called.

If frames is present, the enitre op array must be
applied frames time. At the end of each frame iteration
save the current screen to a file named the
provided basename plus a numeric string such that the
files will be listed in order, then clear the screen and
reset any other data structures that need it.

Important note: you cannot just name your files in
regular sequence, like pic0, pic1, pic2, pic3... if that
is done, then pic1, pic10, pic11... will come before pic2
and so on. In order to keep things clear, add leading 0s
to the numeric portion of the name. If you use sprintf,
you can use "%0xd" for this purpose. It will add at most
x 0s in front of a number, if needed, so if used correctly,
and x = 4, you would get numbers like 0001, 0002, 0011,
0487

05/17/12 09:41:35
jdyrlandweaver
====================*/
void my_main( int polygons ) {

  int i, f, j;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;

  struct vary_node **knobs;
  struct vary_node *vn;
  char frame_name[128];

  num_frames = 1;
  step = 5;
  f=0;

  g.red = 0;
  g.green = 255;
  g.blue = 255;

  first_pass();
  knobs = second_pass();
  for(f = 0;f<num_frames; f++){
    struct vary_node *curr = (struct vary_node *)calloc(1, sizeof(struct vary_node));
    curr = knobs[f];
    while(curr){
      printf("%s : %f\n", curr->name, curr->value);
      curr = curr->next;
    }
  }
  for(f=0;f<num_frames;f++){
    s = new_stack();
    tmp = new_matrix(4, 4);
    transform = new_matrix(4, 4);
    if(num_frames>1){
      vn = knobs[f];
      while(vn){
        SYMTAB *symb = lookup_symbol(vn->name);
        set_value(symb, vn->value);
        vn = vn->next;
      }
    }
    //print_knobs();
    for (i=0;i<lastop;i++) {
      SYMTAB *v;
      switch (op[i].opcode) {
        case SPHERE:
          //printf("Add sphere\n");
          add_sphere( tmp,op[i].op.sphere.d[0], //cx
            op[i].op.sphere.d[1],  //cy
            op[i].op.sphere.d[2],  //cz
            op[i].op.sphere.r,
            step);
          //apply the current top origin
          matrix_mult( s->data[ s->top ], tmp );
          draw_polygons( tmp, t, g );
          tmp->lastcol = 0;
          break;

        case TORUS:
          //printf("Add Torus\n");
          add_torus( tmp, op[i].op.torus.d[0], //cx
            op[i].op.torus.d[1],     //cy
            op[i].op.torus.d[2],    //cz
            op[i].op.torus.r0,
            op[i].op.torus.r1,
            step);
          matrix_mult( s->data[ s->top ], tmp );
          draw_polygons( tmp, t, g );
            tmp->lastcol = 0;
          break;

        case BOX:
          //printf("Add box\n");
          add_box( tmp, op[i].op.box.d0[0],
            op[i].op.box.d0[1],
            op[i].op.box.d0[2],
            op[i].op.box.d1[0],
            op[i].op.box.d1[1],
            op[i].op.box.d1[2]);
          matrix_mult( s->data[ s->top ], tmp );
          draw_polygons( tmp, t, g );
            tmp->lastcol = 0;
          break;

        case LINE:
          //printf("Line\n");
          add_edge( tmp, op[i].op.line.p0[0],
            op[i].op.line.p0[1],
            op[i].op.line.p0[1],
            op[i].op.line.p1[0],
            op[i].op.line.p1[1],
            op[i].op.line.p1[1]);
          draw_lines( tmp, t, g );
            tmp->lastcol = 0;
          break;

        case MOVE:
          //printf("Move\n");
          //get the factors
          xval = op[i].op.move.d[0];
          yval =  op[i].op.move.d[1];
          zval = op[i].op.move.d[2];
          v = op[i].op.move.p;
          if(v){
            xval = xval * v->s.value;
            yval = yval * v->s.value;
            zval = zval * v->s.value;
          }
          //printf("x: %f y: %f z: %f\n", xval, yval, zval);

          transform = make_translate( xval, yval, zval );
          //multiply by the existing origin
          matrix_mult( s->data[ s->top ], transform );
          //put the new matrix on the top
          copy_matrix( transform, s->data[ s->top ] );
          free_matrix( transform );
          break;

        case SCALE:
          //printf("Scale\n");
          xval = op[i].op.scale.d[0];
          yval = op[i].op.scale.d[1];
          zval = op[i].op.scale.d[2];

          v = op[i].op.scale.p;
          if(v){
            //printf("I'm not null Scale\n");
            xval *= v->s.value;
            yval *= v->s.value;
            zval *= v->s.value;
          }

          transform = make_scale( xval, yval, zval );
          matrix_mult( s->data[ s->top ], transform );
          //put the new matrix on the top
          copy_matrix( transform, s->data[ s->top ] );
          free_matrix( transform );
          break;

        case ROTATE:
          //printf("Rotate\n");
          xval = op[i].op.rotate.degrees * ( M_PI / 180 );

          v = op[i].op.rotate.p;
          if(v){
            xval *= v->s.value;
          }
          //get the axis
          if ( op[i].op.rotate.axis == 0 )
            transform = make_rotX( xval );
          else if ( op[i].op.rotate.axis == 1 )
            transform = make_rotY( xval );
          else if ( op[i].op.rotate.axis == 2 )
            transform = make_rotZ( xval );

          matrix_mult( s->data[ s->top ], transform );
          //put the new matrix on the top
          copy_matrix( transform, s->data[ s->top ] );
          free_matrix( transform );
          break;

        case PUSH:
          //printf("Push\n");
          push( s );
          break;
        case POP:
          //printf("Pop\n");
          pop( s );
          break;
        case SAVE:
          //printf("Save\n");
          save_extension( t, op[i].op.save.p->name );
          break;
        case DISPLAY:
          //printf("Display\n");
          display( t );
          break;
      }
    }
    if(num_frames>1){
      sprintf (frame_name, "%s%03d.png", name, f);
      save_extension(t, frame_name);
    }
    clear_screen(t);
    free_stack(s);
    free_matrix(tmp);
  }

  //free_stack( s );
  //free_matrix( tmp );
  //free_matrix( transform );
}
示例#26
0
文件: main.c 项目: stuydw/line
int main() {

  screen s;
  color c;
 
  
  c.red = 0;
  c.green = 0;
  c.blue = 0;
  
  clear_screen(s);

  int i, j;

  for (i=0; i < YRES; i++)
    for (j=0; j < XRES; j++ )
      plot(s, c, i, j);
  
   c.green = MAX_COLOR;
  draw_line(250, 0, 250, 500, s, c);
  draw_line(125, 0, 375, 500, s, c);
  draw_line(0, 0, 500, 500, s, c);
  draw_line(0, 125, 500, 375, s, c);
  draw_line(0, 250, 500, 250, s, c);
  draw_line(0, 375, 500, 125, s, c);
  draw_line(0, 500, 500, 0, s, c);
  draw_line(125, 500, 375, 0, s, c);

  /*  c.green = 0;
  c.red = MAX_COLOR;

  draw_line(125, 0, 125, 250, s, c);
  draw_line(62, 0, 187, 250, s, c);
  draw_line(0, 0, 250, 250, s, c);
  draw_line(0, 62, 250, 187, s, c);
  draw_line(0, 125, 250, 125, s, c);
  draw_line(0, 187, 250, 62, s, c);
  draw_line(0, 250, 250, 0, s, c);
  draw_line(62, 250, 187, 0, s, c);

  c.red = 0;
  c.blue = MAX_COLOR;

  draw_line(375, 0, 375, 250, s, c);
  draw_line(312, 0, 427, 250, s, c);
  draw_line(250, 0, 500, 250, s, c);
  draw_line(250, 62, 500, 187, s, c);
  draw_line(250, 125, 500, 125, s, c);
  draw_line(250, 187, 500, 62, s, c);
  draw_line(250, 250, 500, 0, s, c);
  draw_line(312, 250, 427, 0, s, c);

  c.red = MAX_COLOR;

  draw_line(125, 250, 125, 500, s, c);
  draw_line(62, 250, 187, 500, s, c);
  draw_line(0, 250, 250, 500, s, c);
  draw_line(0, 312, 250, 427, s, c);
  draw_line(0, 375, 500, 375, s, c);
  draw_line(0, 427, 250, 312, s, c);
  draw_line(0, 500, 250, 250, s, c);
  draw_line(62, 500, 187, 250, s, c);

  c.blue = 0;
  c.green = MAX_COLOR;

  draw_line(375, 250, 375, 500, s, c);
  draw_line(312, 250, 427, 500, s, c);
  draw_line(250, 500, 500, 250, s, c);
  draw_line(250, 312, 500, 427, s, c);
  draw_line(250, 375, 500, 375, s, c);
  draw_line(250, 427, 500, 312, s, c);
  draw_line(250, 250, 500, 500, s, c);
  draw_line(312, 500, 427, 250, s, c);
  */

  //Note: Display may not work on your system
  //save_ppm and save_extension should be fine
  //display(s);
  save_ppm(s, "pic.ppm");
  save_extension(s, "whatevs.png");
}  
示例#27
0
文件: my_main.c 项目: stuydw/mdl
void my_main( int polygons ) {

  int i;
  double step;
  double xval, yval, zval;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  
  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {
      //simple cases
      //i realized that you already defined these constants  in another 
      //file after looking at this for 30 min -_-
    case POP:
      pop(s);
      break;
    case PUSH:
      push(s);
      break;

      //move,scale,rotate
    case MOVE:
      transform = make_translate(op[i].op.move.d[0],op[i].op.move.d[1],
				 op[i].op.move.d[2]);
      matrix_mult(transform,s->data[s->top]);
      free_matrix(transform);
      break;
    case ROTATE:
      //there are 3 rotations possible, SO SWITCH-CEPTION!
      switch((int)op[i].op.rotate.axis)
	{
	case ROT_X:
	  transform = make_rotX(op[i].op.rotate.degrees);
	  break;
	case ROT_Y:
	  transform = make_rotY(op[i].op.rotate.degrees);
	  break;
	case ROT_Z:
	  transform = make_rotZ(op[i].op.rotate.degrees);
	  break;
	}
      matrix_mult(transform,s->data[s->top]);
      free_matrix(transform);
      break;
    case SCALE:
      transform = make_scale(op[i].op.scale.d[0],op[i].op.scale.d[1],
			     op[i].op.scale.d[2]);
      matrix_mult(transform,s->data[s->top]);
      free_matrix(transform);
      break;
    
      //box,sphere,torus
    case BOX:
      add_box(tmp,op[i].op.box.d0[0],op[i].op.box.d0[1],op[i].op.box.d0[2],
	      op[i].op.box.d1[0],op[i].op.box.d1[1],op[i].op.box.d1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      free_matrix(tmp);
      //reset
      tmp=new_matrix(4,1000);
      break;
    case SPHERE:
      add_sphere(tmp,op[i].op.sphere.d[0],op[i].op.sphere.d[1],
		 op[i].op.sphere.d[2],op[i].op.sphere.r,0.01);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      free_matrix(tmp);
      //reset
      tmp=new_matrix(4,1000);
      break;
    case TORUS:
      add_torus(tmp,op[i].op.torus.d[0],op[i].op.torus.d[1],op[i].op.torus.d[2],
		op[i].op.torus.r0,op[i].op.torus.r1,0.01);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      free_matrix(tmp);
      //reset
      tmp=new_matrix(4,1000);
      break;
      
      //line
    case LINE:
      add_edge(tmp,op[i].op.line.p0[0],op[i].op.line.p0[1],op[i].op.line.p0[2],
	       op[i].op.line.p1[0],op[i].op.line.p1[1],op[i].op.line.p1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_lines(tmp,t,g);
      free_matrix(tmp);
      tmp=new_matrix(4,1000);//RESET
      break;
     
      //EVERYTIN else
    case SAVE:
      save_extension(t,op[i].op.save.p->name);
      break;
    case DISPLAY:
      display(t);
      break;
    default:
      break;
    }
  }
}
示例#28
0
文件: main.c 项目: stuydw/matrix
int main() {

  screen s;
  color c;

  // TEST: add_point
  struct matrix *a;
  a = (struct matrix *)new_matrix(3,3);
  add_point(a,1,0,2);

  // TEST: print_matrix
  printf("\nAdding one point to matrix A:\n");
  print_matrix(a);
  
  //  TEST: add_edge
  add_edge(a,4,5,3,2,2,2);
  printf("Adding an edge:\n");
  print_matrix(a);

  // TEST: scalar multiplication
  scalar_mult(5,a);
  printf("Scalar multiplication by 5:\n");
  print_matrix(a);
  scalar_mult(0.2,a);
  printf("Scalar multiplication by 0.2, returning to original matrix:\n");
  print_matrix(a);

  // TEST: matrix matrix multiplication
  struct matrix *b;
  b = (struct matrix *)new_matrix(3,1);
  add_point(b,1,2,3);
  printf("Matrix B:\n");
  print_matrix(b);
  matrix_mult(a,b);
  printf("Matrix multiplication of A with B:\n");
  print_matrix(a);

  // TEST: turning a matrix into the identity matrix
  struct matrix *d;
  d = (struct matrix *)new_matrix(3,3);
  add_edge(d,3,4,5,8.2,9.1,2);
  add_point(d,5,2,1);
  printf("Matrix C:\n");
  print_matrix(d);
  ident(d);
  printf("Matrix C after being made into the 3 x 3 identity matrix:\n");
  print_matrix(d);

  /*
  struct matrix *m;
  m = (struct matrix *)new_matrix(3,3);
  m->m[0][0] = 1;
  m->m[0][1] = 2;
  m->m[0][2] = 5;
  m->m[1][0] = 3;
  m->m[1][1] = 4;
  m->m[1][2] = 6;
  m->m[2][0] = 7;
  m->m[2][1] = 8;
  m->m[2][2] = 9;
  */

  /*
  struct matrix *n;
  n = (struct matrix *)new_matrix(3,4);
  n->m[0][0] = 1;
  n->m[1][0] = 2;
  n->m[2][0] = 1;
  n->m[0][1] = 1;
  n->m[1][1] = 0;
  n->m[2][1] = 1;
  n->m[0][2] = 1;
  n->m[1][2] = 2;
  n->m[2][2] = 1;
  n->m[0][3] = 1;
  n->m[1][3] = 1;
  n->m[2][3] = 0;
  */
 
  /*
  struct matrix *p;
  p = (struct matrix *)new_matrix(3,5);
  p->m[0][0] = 1;
  //print_matrix(p);
  //print_matrix(matrix_mult(m,n));
  */

  /*
  struct matrix *q;
  q = (struct matrix *)new_matrix(3,2);
  add_point(q,0,0,0);
  add_point(q,100,0,0);

  struct matrix *t;
  t = (struct matrix *)new_matrix(3,3);
  t->m[0][0] = cos(M_PI / 6);
  t->m[1][0] = sin(M_PI / 6);
  t->m[0][1] = -1 * sin(M_PI / 6);
  t->m[1][1] = cos(M_PI / 6);
  t->lastcol = 2;
  */
  /*add_edge(t,cos( M_PI / 6),sin( M_PI / 6), 0,
	   -1 * sin(M_PI / 6), cos(M_PI / 6), 0);
  */

  /*
  struct matrix *u;
  u = (struct matrix *)new_matrix(3,2);
  add_edge(u,0,0,0,100,0,0);
  
  int i;
  for(i = 0; i < 11; i++){
    matrix_mult(t,q);
    add_edge(u,q->m[0][0],q->m[1][0],q->m[2][0],
	     q->m[0][1],q->m[1][1],q->m[2][1]);}
  int j,k;
  float f;
  for(j = 0; j < u->rows; j++){
    for(k = 0; k < u->cols; k++){
      f = u->m[j][k];
      u->m[j][k] = (int)f + 250;}
  }

  print_matrix(t);
  printf("%f\n", cos(M_PI / 6));
  */

  /*
  struct matrix *r;
  r = (struct matrix *)new_matrix(3,2);
  r->m[0][0] = 0;
  r->m[0][1] = 0;
  r->m[1][0] = 50;
  r->m[1][1] = 50;

  print_matrix(m);
  matrix_mult(m,q);
  print_matrix(m);
  */

  /*
  int i,j;
  c.red = 0;
  c.green = 0;
  c.blue = 255;
  for(i = 0; i < XRES; i++){
    for(j = 0; j < YRES; j++){
      plot(s,c,i,j);
    }
  }
  c.red = 0;
  c.green = 0;
  c.blue = 0;
  draw_lines(q,s,c);
  */

  /*
  int i, j;

  for( i=0; i<XRES; i++) 
    for ( j=0; j<YRES; j++) {

      c.red = random() % (MAX_COLOR + 1);
      c.green = random() % (MAX_COLOR + 1);
      c.blue = random() % (MAX_COLOR + 1);

      plot( s, c, i, j);
    }
  */

  c.red = 170;
  c.green = 240;
  c.blue = 30;

  int i,j;
  
  for(i = 0; i < XRES; i++){
    for(j = 0; j < YRES; j++){
      plot(s,c,i,j);
    }
  }

  // TEST: draw lines in edge matrix
  c.red = 0;
  c.green = 0;
  c.blue = 0;

  struct matrix *e;
  e = (struct matrix *)new_matrix(3,2);
  //add_point(e,0,0,0);
  //add_point(e,250,250,0);
  //draw_lines(e,s,c);
  for(i = 0; i < 25; i++){
    add_edge(e,250+5*i,250+5*i,0,500-5*i,250+5*i,0);
    add_edge(e,500-5*i,250+5*i,0,500-5*i,500-5*i,0);
    //add_edge(e,500-5*i,500-5*i,0,250+5*i,250+5*i,0);
  }
  for(i = 0; i < 25; i++){
    add_edge(e,5*i,5*i,0,250-5*i,5*i,0);
    add_edge(e,250-5*i,5*i,0,250-5*i,250-5*i,0);
    //add_edge(e,250-5*i,250-5*i,0,5*i,5*i,0);
  }
  for(i = 0; i < 25; i++){
    add_edge(e,125+5*i,125+5*i,0,125+5*i,375-5*i,0);
    add_edge(e,125+5*i,375-5*i,0,375-5*i,375-5*i,0);
    //add_edge(e,375-5*i,375-5*i,0,125+5*i,125+5*i,0);
  }
  add_edge(e,0,0,0,125,125,0);
  add_edge(e,375,375,0,500,500,0);
  draw_lines(e,s,c);

  display( s );    
  save_ppm(s,  "image" );
  save_extension(s, "image.jpg");  
  
}  
示例#29
0
/*======== void my_main() ==========
  Inputs:   int polygons  
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void
my_main (int polygons)
{

  int i, f, j;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  char q;

  num_frames = 1;
  step = 0.05;

  g.red = 0;
  g.green = 255;
  g.blue = 255;

  s = new_stack ();
  tmp = new_matrix (4, 1000);
  clear_screen (t);

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

      switch (op[i].opcode)
	{

	case SPHERE:
	  add_sphere (tmp, op[i].op.sphere.d[0],	//cx
		      op[i].op.sphere.d[1],	//cy
		      op[i].op.sphere.d[2],	//cz
		      op[i].op.sphere.r, step);
	  //apply the current top origin
	  matrix_mult (s->data[s->top], tmp);
	  draw_polygons (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case TORUS:
	  add_torus (tmp, op[i].op.torus.d[0],	//cx
		     op[i].op.torus.d[1],	//cy
		     op[i].op.torus.d[2],	//cz
		     op[i].op.torus.r0, op[i].op.torus.r1, step);
	  matrix_mult (s->data[s->top], tmp);
	  draw_polygons (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case BOX:
	  add_box (tmp, op[i].op.box.d0[0],
		   op[i].op.box.d0[1],
		   op[i].op.box.d0[2],
		   op[i].op.box.d1[0],
		   op[i].op.box.d1[1], op[i].op.box.d1[2]);
	  matrix_mult (s->data[s->top], tmp);
	  draw_polygons (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case LINE:
	  add_edge (tmp, op[i].op.line.p0[0],
		    op[i].op.line.p0[1],
		    op[i].op.line.p0[1],
		    op[i].op.line.p1[0],
		    op[i].op.line.p1[1], op[i].op.line.p1[1]);
	  draw_lines (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case MOVE:
	  //get the factors
	  xval = op[i].op.move.d[0];
	  yval = op[i].op.move.d[1];
	  zval = op[i].op.move.d[2];

	  transform = make_translate (xval, yval, zval);
	  //multiply by the existing origin
	  matrix_mult (s->data[s->top], transform);
	  //put the new matrix on the top
	  copy_matrix (transform, s->data[s->top]);
	  free_matrix (transform);
	  break;

	case SCALE:
	  xval = op[i].op.scale.d[0];
	  yval = op[i].op.scale.d[1];
	  zval = op[i].op.scale.d[2];

	  transform = make_scale (xval, yval, zval);
	  matrix_mult (s->data[s->top], transform);
	  //put the new matrix on the top
	  copy_matrix (transform, s->data[s->top]);
	  free_matrix (transform);
	  break;

	case ROTATE:
	  xval = op[i].op.rotate.degrees * (M_PI / 180);

	  //get the axis
	  if (op[i].op.rotate.axis == 0)
	    transform = make_rotX (xval);
	  else if (op[i].op.rotate.axis == 1)
	    transform = make_rotY (xval);
	  else if (op[i].op.rotate.axis == 2)
	    transform = make_rotZ (xval);

	  matrix_mult (s->data[s->top], transform);
	  //put the new matrix on the top
	  copy_matrix (transform, s->data[s->top]);
	  free_matrix (transform);
	  break;

	case PUSH:
	  push (s);
	  break;
	case POP:
	  pop (s);
	  break;
	case SAVE:
	  save_extension (t, op[i].op.save.p->name);
	  break;
	case DISPLAY:
	  display (t);
	  break;
	}
    }

  free_stack (s);
  free_matrix (tmp);
  //free_matrix( transform );    
}
示例#30
0
文件: my_main.c 项目: stuydw/final
/*======== void my_main() ==========
  Inputs:   int polygons  
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void my_main( int polygons ) {
 
  int i, f, j, k;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  char q;
  char dir[256]; 
  char p[256]; 

  num_frames = 1; 
  step = 0.05        ;
 
  g.red = 0;
  g.green = 255;
  g.blue = 255;

  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  first_pass(); 
  if (num_frames == -1)
    return; 

  int variable; 
  struct vary_node **table; 
  table = second_pass(); 
  struct vary_node* inside; 

  for (variable = 0; variable < num_frames; variable++){
    clear_screen(t); 
    free_stack(s); 
    s = new_stack(); 
    inside = table[variable]; 

    while(inside){
      set_value(lookup_symbol(inside->name),inside->value);  
      inside = inside -> next; 
    }
      
    for (i=0;i<lastop;i++) {
  
      switch (op[i].opcode) {
    
      case SPHERE:
	add_sphere( tmp,op[i].op.sphere.d[0], //cx
		    op[i].op.sphere.d[1],  //cy
		    op[i].op.sphere.d[2],  //cz
		    op[i].op.sphere.r,
		    step);
	//apply the current top origin
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case TORUS:
	add_torus( tmp, op[i].op.torus.d[0], //cx
		   op[i].op.torus.d[1],     //cy
		   op[i].op.torus.d[2],    //cz
		   op[i].op.torus.r0,
		   op[i].op.torus.r1,
		   step);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case BOX:
	add_box( tmp, op[i].op.box.d0[0],
		 op[i].op.box.d0[1],
		 op[i].op.box.d0[2],
		 op[i].op.box.d1[0],
		 op[i].op.box.d1[1],
		 op[i].op.box.d1[2]);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case LINE:
	add_edge( tmp, op[i].op.line.p0[0],
		  op[i].op.line.p0[1],
		  op[i].op.line.p0[1],
		  op[i].op.line.p1[0],
		  op[i].op.line.p1[1],
		  op[i].op.line.p1[1]);
	draw_lines( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case MOVE:
	//get the factors
	if( op[ i ].op.move.p )
	  knob_value = lookup_symbol( op[ i ].op.move.p->name )->s.value;
	else
	  knob_value = 1;

	xval = op[i].op.move.d[0] * knob_value;
	yval = op[i].op.move.d[1] * knob_value;
	zval = op[i].op.move.d[2] * knob_value;

	transform = make_translate( xval, yval, zval );
	//multiply by the existing origin
	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;

      case SCALE:
	if( op[ i ].op.scale.p )
	  knob_value = lookup_symbol( op[ i ].op.scale.p->name )->s.value;
	else
	  knob_value = 1;
	xval = op[i].op.scale.d[0] * knob_value;
	yval = op[i].op.scale.d[1] * knob_value;
	zval = op[i].op.scale.d[2] * knob_value;
      
	transform = make_scale( xval, yval, zval );
	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;

      case ROTATE:
	if( op[ i ].op.rotate.p )
	  knob_value = lookup_symbol( op[ i ].op.rotate.p->name )->s.value;
	else
	  knob_value = 1;

	xval = op[i].op.rotate.degrees * ( M_PI / 180 );

	//get the axis
	if ( op[i].op.rotate.axis == 0 ) 
	  transform = make_rotX( xval * knob_value );
	else if ( op[i].op.rotate.axis == 1 ) 
	  transform = make_rotY( xval * knob_value );
	else if ( op[i].op.rotate.axis == 2 ) 
	  transform = make_rotZ( xval * knob_value );

	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;

      case PUSH:
	push( s );
	break;
      case POP:
	pop( s );
	break;
      case SAVE:
	save_extension( t, op[i].op.save.p->name );
	break;
      case DISPLAY:
	display( t );
	break;
      case SET:
	set_value( lookup_symbol( op[ i ].op.set.p->name ), op[ i ].op.set.val );
	break;
      case SETKNOBS:
	for( k = 0; k < lastsym; k++ )
	  if( symtab[ k ].type == SYM_VALUE )
	    symtab[ k ].s.value = op[ i ].op.setknobs.value;
	break;
      default:
	break;
      }
       
    }
      
    strcpy(dir,name);
    sprintf(p,"%03d", variable);
    strcat(dir,p);
    strcat(dir, ".png");
    save_extension(t, dir);
    printf("%s \n", dir);
  }

  free(table); 
  free_stack( s );
  free_matrix( tmp );
  //free_matrix( transform );    
 


}