/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

  1. package us.versus.them.weeds;
  2. import flash.display.Graphics;
  3. import flash.events.Event;
  4. import flash.events.MouseEvent;
  5. import flash.geom.Point;
  6. import flash.Lib;
  7. import flash.text.TextField;
  8. class LineTest {
  9. var line_0_x0 : Float;
  10. var line_0_y0 : Float;
  11. var line_0_x1 : Float;
  12. var line_0_y1 : Float;
  13. var line_1_x0 : Float;
  14. var line_1_y0 : Float;
  15. var line_1_x1 : Float;
  16. var line_1_y1 : Float;
  17. var text : TextField;
  18. static var LINE_0_COLOR = 0x0000FF;
  19. static var LINE_1_COLOR = 0x00FF00;
  20. static var LINE_0_NAME = 'blue';
  21. static var LINE_1_NAME = 'green';
  22. public function new() {
  23. setup();
  24. test();
  25. Lib.current.stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseDown );
  26. }
  27. public function setup() {
  28. text = new TextField();
  29. text.x = 0;
  30. text.y = 0;
  31. text.width = 1024;
  32. Lib.current.stage.addChild( text );
  33. var tmp = new TextField();
  34. tmp.x = 500;
  35. tmp.y = 450;
  36. tmp.width = 200;
  37. tmp.text = 'click to run another test';
  38. Lib.current.stage.addChild( tmp );
  39. }
  40. public function test() {
  41. line_0_x0 = 640 * Math.random();
  42. line_0_x1 = 640 * Math.random();
  43. line_1_x0 = 640 * Math.random();
  44. line_1_x1 = 640 * Math.random();
  45. line_0_y0 = 480 * Math.random();
  46. line_0_y1 = 480 * Math.random();
  47. line_1_y0 = 480 * Math.random();
  48. line_1_y1 = 480 * Math.random();
  49. drawLoop();
  50. }
  51. public function drawLoop() {
  52. this.draw( flash.Lib.current.graphics );
  53. }
  54. public function draw( graphics:Graphics ) {
  55. graphics.clear();
  56. drawIntersection( graphics );
  57. drawLines( graphics );
  58. }
  59. public function drawLines( graphics:Graphics ) {
  60. graphics.lineStyle( 2, LINE_0_COLOR );
  61. graphics.moveTo( line_0_x0, line_0_y0 );
  62. graphics.lineTo( line_0_x1, line_0_y1 );
  63. graphics.lineStyle( 2, LINE_1_COLOR );
  64. graphics.moveTo( line_1_x0, line_1_y0 );
  65. graphics.lineTo( line_1_x1, line_1_y1 );
  66. }
  67. public function drawIntersection( graphics:Graphics ) {
  68. var t_0 = App.intersectionTime(
  69. line_0_x0, line_0_y0, line_0_x1, line_0_y1
  70. ,
  71. line_1_x0, line_1_y0, line_1_x1, line_1_y1
  72. );
  73. var t_1 = App.intersectionTime(
  74. line_1_x0, line_1_y0, line_1_x1, line_1_y1
  75. ,
  76. line_0_x0, line_0_y0, line_0_x1, line_0_y1
  77. );
  78. var hit_on_0 = ( t_0 >= 0 && t_0 <= 1 );
  79. var hit_on_1 = ( t_1 >= 0 && t_1 <= 1 );
  80. var real_hit = hit_on_0 && hit_on_1;
  81. var x = line_0_x0 + ( line_0_x1 - line_0_x0 ) * t_0;
  82. var y = line_0_y0 + ( line_0_y1 - line_0_y0 ) * t_0;
  83. graphics.lineStyle( 1, 0x000000 );
  84. if ( real_hit ) {
  85. graphics.beginFill( 0xFF0000 );
  86. graphics.drawCircle( x, y, 8 );
  87. } else {
  88. graphics.beginFill( 0xFFFFFF );
  89. }
  90. graphics.drawCircle( x, y, 4 );
  91. graphics.endFill();
  92. text.text = ( ''
  93. + 'on the '
  94. + LINE_0_NAME + ' line at t=' + clip( t_0 )
  95. + ' and on '
  96. + LINE_1_NAME + ' line at t=' + clip( t_1 )
  97. );
  98. if ( real_hit ) {
  99. text.text += ' on both line segments';
  100. } else {
  101. if ( hit_on_0 ) {
  102. text.text += ' on the segment for the ' + LINE_0_NAME + ' line';
  103. } else {
  104. if ( hit_on_1 ) {
  105. text.text += ' on the segment for the ' + LINE_1_NAME + ' line';
  106. } else {
  107. text.text += ' so on neither segment';
  108. }
  109. }
  110. }
  111. }
  112. public function clip( f:Float ) {
  113. return ~/^([^.]*\...).*/.replace( '' + f, '$1' );
  114. }
  115. public function mouseDown( event:Event ) {
  116. test();
  117. }
  118. }