/us/versus/them/weeds/LineTest.hx
http://brianin3d-weeds.googlecode.com/ · Haxe · 144 lines · 118 code · 26 blank · 0 comment · 13 complexity · 0a663b63272963ffa337299297479ebc MD5 · raw file
- package us.versus.them.weeds;
- import flash.display.Graphics;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.geom.Point;
- import flash.Lib;
- import flash.text.TextField;
- class LineTest {
- var line_0_x0 : Float;
- var line_0_y0 : Float;
- var line_0_x1 : Float;
- var line_0_y1 : Float;
- var line_1_x0 : Float;
- var line_1_y0 : Float;
- var line_1_x1 : Float;
- var line_1_y1 : Float;
- var text : TextField;
- static var LINE_0_COLOR = 0x0000FF;
- static var LINE_1_COLOR = 0x00FF00;
- static var LINE_0_NAME = 'blue';
- static var LINE_1_NAME = 'green';
- public function new() {
- setup();
- test();
- Lib.current.stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseDown );
- }
- public function setup() {
- text = new TextField();
- text.x = 0;
- text.y = 0;
- text.width = 1024;
- Lib.current.stage.addChild( text );
- var tmp = new TextField();
- tmp.x = 500;
- tmp.y = 450;
- tmp.width = 200;
- tmp.text = 'click to run another test';
- Lib.current.stage.addChild( tmp );
- }
- public function test() {
- line_0_x0 = 640 * Math.random();
- line_0_x1 = 640 * Math.random();
- line_1_x0 = 640 * Math.random();
- line_1_x1 = 640 * Math.random();
- line_0_y0 = 480 * Math.random();
- line_0_y1 = 480 * Math.random();
- line_1_y0 = 480 * Math.random();
- line_1_y1 = 480 * Math.random();
- drawLoop();
- }
- public function drawLoop() {
- this.draw( flash.Lib.current.graphics );
- }
- public function draw( graphics:Graphics ) {
- graphics.clear();
- drawIntersection( graphics );
- drawLines( graphics );
- }
- public function drawLines( graphics:Graphics ) {
- graphics.lineStyle( 2, LINE_0_COLOR );
- graphics.moveTo( line_0_x0, line_0_y0 );
- graphics.lineTo( line_0_x1, line_0_y1 );
- graphics.lineStyle( 2, LINE_1_COLOR );
- graphics.moveTo( line_1_x0, line_1_y0 );
- graphics.lineTo( line_1_x1, line_1_y1 );
- }
- public function drawIntersection( graphics:Graphics ) {
- var t_0 = App.intersectionTime(
- line_0_x0, line_0_y0, line_0_x1, line_0_y1
- ,
- line_1_x0, line_1_y0, line_1_x1, line_1_y1
- );
- var t_1 = App.intersectionTime(
- line_1_x0, line_1_y0, line_1_x1, line_1_y1
- ,
- line_0_x0, line_0_y0, line_0_x1, line_0_y1
- );
- var hit_on_0 = ( t_0 >= 0 && t_0 <= 1 );
- var hit_on_1 = ( t_1 >= 0 && t_1 <= 1 );
- var real_hit = hit_on_0 && hit_on_1;
- var x = line_0_x0 + ( line_0_x1 - line_0_x0 ) * t_0;
- var y = line_0_y0 + ( line_0_y1 - line_0_y0 ) * t_0;
- graphics.lineStyle( 1, 0x000000 );
- if ( real_hit ) {
- graphics.beginFill( 0xFF0000 );
- graphics.drawCircle( x, y, 8 );
- } else {
- graphics.beginFill( 0xFFFFFF );
- }
- graphics.drawCircle( x, y, 4 );
- graphics.endFill();
- text.text = ( ''
- + 'on the '
- + LINE_0_NAME + ' line at t=' + clip( t_0 )
- + ' and on '
- + LINE_1_NAME + ' line at t=' + clip( t_1 )
- );
- if ( real_hit ) {
- text.text += ' on both line segments';
- } else {
- if ( hit_on_0 ) {
- text.text += ' on the segment for the ' + LINE_0_NAME + ' line';
- } else {
- if ( hit_on_1 ) {
- text.text += ' on the segment for the ' + LINE_1_NAME + ' line';
- } else {
- text.text += ' so on neither segment';
- }
- }
- }
- }
- public function clip( f:Float ) {
- return ~/^([^.]*\...).*/.replace( '' + f, '$1' );
- }
- public function mouseDown( event:Event ) {
- test();
- }
- }