import java.awt.*;
import java.util.*;
import java.io.*;

public class ThreeDView extends Canvas implements Runnable {
  Vector points = new Vector();
  ThreeDView threedv;
  ThreeDPoint origin, xhat, yhat, zhat;
  private Image offScreenImage;
  private Graphics offScreenGraphics;
  private Dimension offScreenSize;
  double angle1=0.0, angle2=0.0;
  int tmpx, tmpy;
  Rotation rot;

  public static void main(String arg[]){
    new ThreeDView();
  }

  ThreeDView () {
    Frame f = new Frame();
    f.setBounds(0,0,1000,1000);
    f.add(this);
    f.pack();
    f.show();
    ThreeDPoint.setView(this);
    rot = new Rotation(1, 0, 0,
		     0, 1, 0,
		     0, 0, 1);
    origin = new ThreeDPoint(0,0,0, Color.black);
    xhat = new ThreeDPoint(200,0,0, Color.black);
    yhat = new ThreeDPoint(0,200,0, Color.black);
    zhat = new ThreeDPoint(0,0,200, Color.black);
    
    (new Thread(this)).start();
  }

  public void run() {
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st;
    String colname;
    Color thecolor;
    while (true){
      String coords = new String("");
      double x, y, z;
      try { coords = in.readLine(); } catch (Exception e) { } 
      if(coords == null){
	break;
      }
      st = new StringTokenizer(coords); 
      x = (Double.valueOf(st.nextToken())).doubleValue();
      y = (Double.valueOf(st.nextToken())).doubleValue();
      z = Double.valueOf(st.nextToken()).doubleValue();
      colname = st.nextToken();
      if(colname.equals("red"))
	thecolor = Color.red;
      else if(colname.equals("blue"))
	thecolor = Color.blue;
      else
	thecolor = Color.black;
      points.addElement(new ThreeDPoint(x, y, z, thecolor));
    }
    while(true){
      repaint();
      try { Thread.sleep(2000); } catch (Exception e) { }
    }
  }

    public boolean mouseDown(Event ev, int mx, int my){
      tmpx = mx;
      tmpy = my;
      return true;
    }

    public boolean mouseDrag(Event ev, int mx, int my){
      double dx, dy;
      double rotscale = 400;

      dx = (double) (mx-tmpx);
      dy = (double) (my-tmpy);
      rot = rot.times(new Rotation(Math.cos(dx/rotscale), 0, Math.sin(dx/rotscale),
				   0, 1, 0,
				   -1*Math.sin(dx/rotscale), 0, Math.cos(dx/rotscale)));
      rot = rot.times(new Rotation(1, 0, 0,
				   0, Math.cos(dy/rotscale), Math.sin(dy/rotscale),
				   0, -1*Math.sin(dy/rotscale), Math.cos(dy/rotscale)));
      tmpx = mx;
      tmpy = my;
      repaint();
      return true;
    }

  public final synchronized void update (Graphics theG)
    {
      Dimension d = size();
      if((offScreenImage == null) || (d.width != offScreenSize.width) ||
         (d.height != offScreenSize.height)) 
        {
          offScreenImage = createImage(d.width, d.height);
          offScreenSize = d;
          offScreenGraphics = offScreenImage.getGraphics();
          offScreenGraphics.setFont(getFont());
        }
      offScreenGraphics.fillRect(0,0,d.width, d.height);
      paint(offScreenGraphics);
      theG.drawImage(offScreenImage, 0, 0, null);
    }

  public void paint (Graphics g){
    int ct=0;
    g.setColor(Color.black);
    g.drawLine((int) (500-rot.x(origin)), 
	       (int) (500-rot.y(origin)), 
	       (int) (500-rot.x(xhat)), 
	       (int) (500-rot.y(xhat)));
    g.drawLine((int) (500-rot.x(origin)), 
	       (int) (500-rot.y(origin)), 
	       (int) (500-rot.x(yhat)), 
	       (int) (500-rot.y(yhat)));
    g.drawLine((int) (500-rot.x(origin)), 
	       (int) (500-rot.y(origin)), 
	       (int) (500-rot.x(zhat)), 
	       (int) (500-rot.y(zhat)));
    for(Enumeration e = points.elements(); e.hasMoreElements(); ){
      ThreeDPoint gl;
      ct++;
      gl = ((ThreeDPoint) e.nextElement());
      //      g.setColor(new Color((float) ((100+gl.x)/200), 
      //		   (float) ((100+gl.y)/200), 
      //		   (float) ((100+gl.z)/200)));
      gl.draw(g);
    }
    g.setColor(Color.gray);
  }

}

class ThreeDPoint {
  double x, y, z;
  Color c;
  static ThreeDView applet;

  ThreeDPoint(double l1, double l2, double l3, Color col){
    x = l1;
    y = l2;
    z = l3;
    c = col;
  }

  public static void setView(ThreeDView a){
    applet = a;
  }

  public void draw(Graphics g){
    double projx, projy;
    projx = applet.rot.x(this);
    projy = applet.rot.y(this);
    g.setColor(c);
    g.drawArc((int) (500-projx), (int) (500-projy), 2, 2, 0, 360);
  }
}

class Rotation {
  double a, b, c, d, e, f, g, h, i;

  Rotation(double r1, double r2, double r3, 
	   double r4, double r5, double r6, 
	   double r7, double r8, double r9){
    
    a = r1;
    b = r2;
    c = r3;
    d = r4;
    e = r5;
    f = r6;
    g = r7;
    h = r8;
    i = r9;
  }

  public Rotation times(Rotation r){
    Rotation t;
    t = new Rotation((a*r.a + b*r.d + c*r.g), (a*r.b + b*r.e + c*r.h), (a*r.c + b*r.f + c*r.i), 
			(d*r.a + e*r.d + f*r.g), (d*r.b + e*r.e + f*r.h), (d*r.c + e*r.f + f*r.i),
			(g*r.a + h*r.d + i*r.g), (g*r.b + h*r.e + i*r.h), (g*r.c + h*r.f + i*r.i));
    t = new Rotation((r.a*a + r.b*d + r.c*g), (r.a*b + r.b*e + r.c*h), (r.a*c + r.b*f + r.c*i), 
			(r.d*a + r.e*d + r.f*g), (r.d*b + r.e*e + r.f*h), (r.d*c + r.e*f + r.f*i),
			(r.g*a + r.h*d + r.i*g), (r.g*b + r.h*e + r.i*h), (r.g*c + r.h*f + r.i*i));
    //    return t.times(t.det());
    return t;
  }

  public Rotation times(double t){
    System.out.println("Determinant is: "+t);
    return new Rotation(a*t, b*t, c*t,
			d*t, e*t, f*t,
			g*t, h*t, i*t);
  }


  public double det(){
    return((a*e*i)-(a*h*f)-(b*d*i)+(b*g*f)+(c*d*h)-(c*e*g));
  }

  public double x(ThreeDPoint p){
    return (a*p.x + b*p.y + c*p.z);
  }

  public double y(ThreeDPoint p){
    return (d*p.x + e*p.y + f*p.z);
  }
}










