SporkApp Source Code

( <-- Back to SporkApp )

Title:"Polygon Drawing Utility"
Author:jchase
Last updated:
05/02/13  08:18 pm

Source code:


image result;
string data_str;
string err_str;
global int color_incr = 96;


while (1) {

  byte have_result = (err_str=="" && image_wd(result)>0);

  print_format("Polygon Drawing Utility\n\n","b");

  print("Please enter x & y point values, then click the button.\n\n");
  print("You may specify multiple polygons by separating their coordinates\n   with the word \"break\".\n\n\n");

  print_input_button("Draw Polygon(s)");
  if (err_str!="") {
    print("                       Error: " + err_str);
    err_str = "";
  } elseif (have_result) {
    print("                       Result:");
  }
  print ("\n\n");

  input data_inp = print_input_textbox(30,40,data_str);

  if (have_result) {
    print("      ");
    print_image(result);
  }

  print("\n\n");

  load_user_input();

  data_str = get_input_value(data_inp);

  string sep0 = " ";
  string sep1 = chr(10);
  string sep2 = chr(13);
  string sep3 = chr(8);
  string sep4 = "([{}]),;";

  string sep = sep0 + sep1 + sep2 + sep3 + sep4;

  array string toks;

  split(data_str,toks,sep);

  int n = size(toks);

  int i, i2;
  float x, y;
  float max_x = -0x7fffffff;
  float min_x = 0x7fffffff;
  float max_y = -0x7fffffff;
  float min_y = 0x7fffffff;

  array float dat_x, dat_y;
  array int breaks;
  string tok;

  for (i=0; i<n; ) {
    tok = toks[i]; ++i;
    if (tolower(tok)=="break") {
      push_back(breaks,size(dat_x));
      continue;
    }
    x = floatval(tok);
    if (i>=n) {
      err_str = "Unexpected end to array - missing y coord";
      break;
    }
    tok = toks[i]; ++i;
    if (tolower(tok)=="break") {
      err_str = "Unexpected \"break\" token at y-position within coordinate pair";
      break;
    }
    y = floatval(tok);
    push_back(dat_x,x);
    push_back(dat_y,y);
    if (x<min_x) {
      min_x = x;
    }
    if (x>max_x) {
      max_x = x;
    }
    if (y<min_y) {
      min_y = y;
    }
    if (y>max_y) {
      max_y = y;
    }
  }
  
  if (err_str!="") {
    continue;
  }

  n = size(dat_x);
  push_back(breaks,n);

  int pxsz = 600;

  rgb white, black;
  set_rgb_r(white,255);
  set_rgb_g(white,255);
  set_rgb_b(white,255);
  set_rgb_r(black,0);
  set_rgb_g(black,0);
  set_rgb_b(black,0);

  float scale;
  float diffx = max_x - min_x + 1;
  float diffy = max_y - min_y + 1;

  if (diffx>diffy) {
    scale = (pxsz-40)/diffx;
  } else {
    scale = (pxsz-40)/diffy;
  }

  resize_image(result,40+ceil(scale*diffx),40+ceil(scale*diffy));
  set_image_to_color(result,white);

  int j, jn;
  float t, dt, px0, py0, px, py;
  int shapes_itr, shape_endpos=0, last_shape_endpos;
  int num_shapes = size(breaks);
  
  rgb color = black;
  
  for (shapes_itr=0; shapes_itr<num_shapes; ++shapes_itr) {
    last_shape_endpos = shape_endpos;
    shape_endpos = breaks[shapes_itr];
    int n0 = shape_endpos - last_shape_endpos;
    if (n0==0) {
      continue;
    }
    for (i=last_shape_endpos; i<shape_endpos; ++i) {
      px0 = scale*(dat_x[i] - min_x) + 20;
      py0 = scale*(dat_y[i] - min_y) + 20;
      i2 = last_shape_endpos + ((i-last_shape_endpos+1)%n0);
      px = scale*(dat_x[i2] - min_x) + 20;
      py = scale*(dat_y[i2] - min_y) + 20;
      jn = 2*(abs(px-px0) + abs(py-py0));
      if (jn==0) {
        continue;
      }
      dt = 0.5/jn;
      int tx, ty;
      float fx, fy;
      for (t=0.0; t<=1; t+=dt) {
        fx = t*px + (1.0-t)*px0;
        fy = t*py + (1.0-t)*py0;
        tx = round(t*px + (1.0-t)*px0);
        ty = round(t*py + (1.0-t)*py0);
        set_image_p(result,tx,ty,color);
        set_image_p(result,tx+1,ty,color);
        ++ty;
        set_image_p(result,tx,ty,color);
        set_image_p(result,tx+1,ty,color);
      }
    }
    iterate_color(shapes_itr,color);
  }
}

function void iterate_color(int i, rgb & c)
{
  int j = (i % 7)+1;
  int j0 = ((i+6) % 7)+1;
  
  if (j0 & 0x1) {
    set_rgb_b(c,rgb_b(c) - color_incr);
  }
  if (j0 & 0x2) {
    set_rgb_g(c,rgb_g(c) - color_incr);
  }
  if (j0 & 0x4) {
    set_rgb_r(c,rgb_r(c) - color_incr);
  }
  
  if (j & 0x1) {
    set_rgb_b(c,rgb_b(c) + color_incr);
  }
  if (j & 0x2) {
    set_rgb_g(c,rgb_g(c) + color_incr);
  }
  if (j & 0x4) {
    set_rgb_r(c,rgb_r(c) + color_incr);
  }
}









sporkforge