package sample.svg.woodrail;

import static util.SVGUtil.*;


// 1インチ 25.4mm

// http://www.rockler.com/plans/TrainTrackPlan.pdf
// 全体の幅　　　 3/2" (38.1mm)
// 
// male connector 11/16"
// 溝の内側　端から 3/8"(9.525mm) 中心から3/8"(9.525mm)
// 溝の中心　端から 1/4"(6.35mm)  中心から1/2"(12.7mm)
// 溝の外側　端から 1/8"(3.175mm) 中心から5/8"(15.875mm)
// 溝の幅　　　　　 1/4"(6.35mm)
// 溝の深さ 1/8" = 0.3175センチ
//The key is about 11/16"(16.84375mm) long, with a 1/2"(12.7mm) round end and a 1/4"(6.35mm) wide shaft.
//hole 3/4" (19.05mm)

// http://en.wikipedia.org/wiki/Wooden_toy_train
// 全体の幅  38mm
// 溝の幅　	 5mm
// 溝の内側　中心から 10mm
// 溝の中心　中心から 12.5mm
// 溝の外側  中心から 15mm

// 実測値
// キー（♂）全体の長さ 18mm （シャフトの長さ 7mm） 丸部分の直径 11mm   シャフトの幅 5mm
// キー（♀）全体の長さ 18mm （シャフトの長さ 6mm） 丸部分の直径 13.5mm シャフトの幅 7mm

// カーブのレール中止での半径　216mm = 8 1/2"

public class Y30 {
	private static final double R = 406.4;
	private static final double w0 = 19.05;   // 溝の内側
//	private static final double w1 = 25.4;    // 溝の中心
	private static final double w2 = 31.75;   // 溝の外側
	private static final double w3 = 40;    // レールの幅

	private static final double mR = 11;
	private static final double fR = 13;
	private static final double mL = 17.5;
	private static final double fL = 18;
	private static final double mW = 5.5;
	private static final double fW = 7;

	private static void maleConnector() {
		line(mW/2, 0, mW/2, -(mL-mR/2));
		line(-mW/2, 0, -mW/2, -(mL-mR/2));
		ellipse(0, -(mL-mR/2), mR, mR);
	}

	private static void femaleConnector() {
		line(fW/2, 0, fW/2, -(fL-fR/2));
		line(-fW/2, 0, -fW/2, -(fL-fR/2));
		ellipse(0, -(fL-fR/2), fR, fR);	
	}

	public static void main(String[] args) {
		start();                     /* 最初に必要 */
		rulers();
		//		strokeWeight((w2-w0)/2);
		noFill();
		strokeWeight(0.1);
		stroke(bw1(0));

		//		stroke(hsb360(240, 50, 100));

//		
//		pushMatrix();
//		translate(WIDTH/2, 180);
//		c45L();
//		popMatrix();
		
		pushMatrix();
		translate(65, 160);	
		y30();
		popMatrix();

		pushMatrix();
		translate(centerX()/2, 50);
		scale(1, -1);
		y30();
		popMatrix();

		pushMatrix();
		translate(centerX()-65, 160);	
		y30();
		popMatrix();

		finish();                    /* 最後に必要 */
	}

	private static void y30() {
		c30L();	
		c30R();
		c45L();
	}

	private static void c30R() {
		//		arc360(R/2, 0, R-w1, R-w1, 180, 210);
		//		arc360(R/2, 0, R+w1, R+w1, 180, 210);
		arc360(R/2, 0, R-w0, R-w0, 180, 210);
		arc360(R/2, 0, R-w2, R-w2, 180, 210);
		arc360(R/2, 0, R+w0, R+w0, 180, 210);
		arc360(R/2, 0, R+w2, R+w2, 180, 210);
		line((-w3)/2, 0, w3/2, 0);
		line(R/2+(R-w3)/2*cos360(210), (R-w3)/2*sin360(210), R/2+(R+w3)/2*cos360(210), (R+w3)/2*sin360(210));
		arc360(R/2, 0, R-w3, R-w3, 180, 210);
		arc360(R/2, 0, R+w3, R+w3, 180, 210);

		pushMatrix();
		translate(R/2+R/2*cos360(210), R/2*sin360(210));
		rotate360(210);
		stroke(0xff0000);
		femaleConnector();
		stroke(bw1(0));
		popMatrix();

		pushMatrix();
		scale(1, -1);
		stroke(0x0000ff);
		maleConnector();
		stroke(bw1(0));
		popMatrix();
	}

	private static void c30L() {
		//		arc360(-R/2, 0, R-w1, R-w1, -30, 0);
		//		arc360(-R/2, 0, R+w1, R+w1, -30, 0);
		arc360(-R/2, 0, R-w0, R-w0, -30, 0);
		arc360(-R/2, 0, R-w2, R-w2, -30, 0);
		arc360(-R/2, 0, R+w0, R+w0, -30, 0);
		arc360(-R/2, 0, R+w2, R+w2, -30, 0);
		line((-w3)/2, 0, w3/2, 0);
		line(-R/2+(R-w3)/2*cos360(-30), (R-w3)/2*sin360(-30), -R/2+(R+w3)/2*cos360(-30), (R+w3)/2*sin360(-30));
		arc360(-R/2, 0, R-w3, R-w3, -30, 0);	
		arc360(-R/2, 0, R+w3, R+w3, -30, 0);	

		pushMatrix();
		translate(-R/2+R/2*cos360(-30), R/2*sin360(-30));
		rotate360(-30);
		stroke(0x0000ff);
		maleConnector();
		stroke(bw1(0));
		popMatrix();

		pushMatrix();
		stroke(0xff0000);
		femaleConnector();
		stroke(bw1(0));
		popMatrix();
	}
	
	
	private static void c45L() {
		arc360(-R/2, 0, R-w0, R-w0, -45, 0);
		arc360(-R/2, 0, R-w2, R-w2, -45, 0);
		arc360(-R/2, 0, R+w0, R+w0, -45, 0);
		arc360(-R/2, 0, R+w2, R+w2, -45, 0);
		line((-w3)/2, 0, w3/2, 0);
		line(-R/2+(R-w3)/2*cos360(-45), (R-w3)/2*sin360(-45), -R/2+(R+w3)/2*cos360(-45), (R+w3)/2*sin360(-45));
		arc360(-R/2, 0, R-w3, R-w3, -45, 0);	
		arc360(-R/2, 0, R+w3, R+w3, -45, 0);	

		pushMatrix();
		translate(-R/2+R/2*cos360(-45), R/2*sin360(-45));
		rotate360(-45);
		stroke(0x0000ff);
		maleConnector();
		stroke(bw1(0));
		popMatrix();

		pushMatrix();
		stroke(0xff0000);
		femaleConnector();
		stroke(bw1(0));
		popMatrix();
	}
}
