#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define APD 255

double APconv(int ap)
{
  if (ap < 18) return((double)ap * 0.05 + 0.1);
  else return((double)ap * 0.1 - 0.8);
}

void draw_line(double x1, double y1, double x2, double y2, int layer, int ap)
{
  printf("LINE:XS=%.3f,YS=%.3f,XE=%.3f,YE=%.3f,LE=%d,AP=%d\n",
	 x1, y1, x2, y2, layer, ap);
}

double len(double x1, double y1, double x2, double y2)
{
  return(sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)));
}

main(int ac, char **av)
{
  int le, let, led, ap0;
  char line[1024];
  char type[10];
  double x1, y1, x2, y2, ap, w, tmp, ex, ey, nx, ny;
  char delim[] = ":,=\r\n";

  //  let = 5; // SolerMask
  let = 6; // PartsMask
  led = 0;

  printf("[Version]\nPCBE:Version 0.48.4\n[BoardSize]\nX:300\nY:210\nGRID:1\n");
  printf("[LayerDef]\n%d:NAME=Outline,COLOR=0,DISP=ON,ACTIV=ON,PRINT=ON\n", led);
  printf("%d:NAME=Original,COLOR=6,DISP=ON,ACTIV=ON,PRINT=ON\n", let);
  printf("[PadDef]\n[FlshDef]\n[LineDef]\n255:0.001\n[DrawItems]\n");

  type[4] = '\0';
  while(fgets(line, 1023, stdin) != NULL){
    strncpy(type, line, 4);
    if (strcmp(type, "LAND") == 0 || strcmp(type, "FLSH") == 0){
      strtok(line, delim);
      strtok(NULL, delim);
      x1 = atof(strtok(NULL, delim));
      strtok(NULL, delim);
      y1 = atof(strtok(NULL, delim));
      strtok(NULL, delim);
      le = atoi(strtok(NULL, delim));
      strtok(NULL, delim);
      ap0 = atoi(strtok(NULL, delim));
      ap = APconv(ap0);
      w = ap / 2.0;
      if (le == let){
        printf("GROUP:\n");
	printf("%s:X=%.3f,Y=%.3f,LE=%d,AP=%d\n", type, x1, y1, let, ap0);
	draw_line(x1-w, y1-w, x1+w, y1-w, led, APD);
	draw_line(x1+w, y1-w, x1+w, y1+w, led, APD);
	draw_line(x1+w, y1+w, x1-w, y1+w, led, APD);
	draw_line(x1-w, y1+w, x1-w, y1-w, led, APD);
        printf("ENDGR:\n");
      }
    }
    if (strcmp(type, "LINE") == 0){
      strtok(line, delim);
      strtok(NULL, delim);
      x1 = atof(strtok(NULL, delim));
      strtok(NULL, delim);
      y1 = atof(strtok(NULL, delim));
      strtok(NULL, delim);
      x2 = atof(strtok(NULL, delim));
      strtok(NULL, delim);
      y2 = atof(strtok(NULL, delim));
      strtok(NULL, delim);
      le = atoi(strtok(NULL, delim));
      strtok(NULL, delim);
      ap0 = atoi(strtok(NULL, delim));
      ap = APconv(ap0);
      w = ap / 2.0;
      if (le == let){
	ex = (x2 - x1) / len(x1, y1, x2, y2) * w;
	ey = (y2 - y1) / len(x1, y1, x2, y2) * w;
	nx = -ey;
	ny = ex;
        printf("GROUP:\n");
	draw_line(x1, y1, x2, y2, let, ap0);
	draw_line(x1-ex+nx, y1-ey+ny, x2+ex+nx, y2+ey+ny, led, APD);
	draw_line(x1-ex+nx, y1-ey+ny, x1-ex-nx, y1-ey-ny, led, APD);
	draw_line(x1-ex-nx, y1-ey-ny, x2+ex-nx, y2+ey-ny, led, APD);
	draw_line(x2+ex+nx, y2+ey+ny, x2+ex-nx, y2+ey-ny, led, APD);
        printf("ENDGR:\n");
      }
    }
  }
  
}
