Ex15_1



/*-----------
 Ex15_1.java
 対数螺旋
 ------------*/
package jp_bunkyo;

import java.applet.*;
import java.awt.*;

public class Ex15_1 extends Applet {
	double theta, a, b;		// 対数螺旋の回転角θ,定数a,b
	int cx, cy, x1, y1, x2, y2;	// 座標(x1,y1),(x2,y2),螺旋中心位置(cx,cy)
	int red, grn, blu;		// 色設定用:赤・緑・青
	int num, width, height;	// 描画回数num, 幅width, 高さheight
	Color col;

	public void init() {	// 初期化
		this.setSize(500, 500);	// 描画領域 500×500
		cx = 200;	cy = 120;	// 螺旋の開始(中心)位置 (cx, cy) = (200, 120)
		num = 330;				// 線分・楕円の描画回数 330回
		theta = 2*Math.PI / 60;		// 対数螺旋用回転角(1回当たり)= 2π/60 = 360°/60
		a = 1.2;	 b = 0.15;		// 対数螺旋用定数
		red = 0;	grn = 125;	blu = 255;	// 赤・緑・青
		col = new Color(red, grn, blu);		// 色を混ぜて作る
	}

	public void paint(Graphics g) {	// 絵を描くためのメソッド
		for (int i = 0; i < num; i++) {
			red = (red + 10*i) % 255;	// 赤光を10*i強く & 0〜255内にするため剰余計算
			grn = (grn + 20*i) % 255;	// 緑光を20*i強く & 0〜255内にするため剰余計算
			blu = (blu + 30*i) % 255;	// 青光を30*i強く & 0〜255内にするため剰余計算
			col = new Color(red, grn, blu);
			g.setColor(col);

			x1 = cx + (int)(a * Math.exp(b * theta * i) * Math.cos(theta * i));	// 1点目の座標(x1,y1)
			y1 = cy + (int)(a * Math.exp(b * theta * i) * Math.sin(theta * i));
			x2 = cx + (int)(a * Math.exp(b * theta * (i+1)) * Math.cos(theta * (i+1)));	// 2点目の座標(x2,y2)
			y2 = cy + (int)(a * Math.exp(b * theta * (i+1)) * Math.sin(theta * (i+1)));
			g.drawLine(x1, y1, x2, y2);	// 2点(x1,y1)-(x2,y2)間の線分を引く

			width = (int)(a * Math.exp(b * theta * i));	// 楕円の幅
			height= (int)(a * Math.exp(b * theta * i));	// 楕円の高さ
			g.drawOval(x1, y1, width, height);	// 左上(x1,y1)として幅width,高さheightの楕円を描く
		}
	}
}