Guest User

Untitled

a guest
May 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. package
  2. {
  3. import kwm.*;
  4.  
  5. public class Diamond{
  6.  
  7. public static const COLOR_WHITE:uint = 0xffffff;
  8. public static const COLOR_BLACK:uint = 0x000000;
  9.  
  10. public static function main(): void
  11. {
  12. var r:uint; //Größe des Radius
  13. var n:uint; //Anzahl der Facetten (farbig abwechselnde Flächen)
  14. var s:uint; //Anzahl der Segmente ("Spalten")
  15. var centerX:uint = 0; //Mittelpunkt auf x-Achse
  16. var centerY:uint; //Mittelpunkt auf y-Achse
  17. var valid:Boolean = true;
  18.  
  19. while(true){
  20.  
  21.  
  22. r = Input.readUint();
  23.  
  24. if(Input.hasEnded()){
  25. break;
  26. }
  27.  
  28. if(!Input.isOkay()){
  29. valid = false;
  30. Input.clearError();
  31. }
  32. else if(r== 0){
  33. valid = false;
  34. }
  35.  
  36.  
  37.  
  38. n = Input.readUint();
  39.  
  40. if(Input.hasEnded()){
  41. break;
  42. }
  43.  
  44. if(!Input.isOkay()){
  45. valid = false;
  46. Input.clearError();
  47. }
  48.  
  49. else if(n < 4 || n%2 !=0){
  50. valid = false;
  51. }
  52.  
  53.  
  54. s = Input.readUint();
  55.  
  56. if(Input.hasEnded()){
  57. break;
  58. }
  59.  
  60. if(!Input.isOkay()){
  61. valid = false;
  62. Input.clearError();
  63. }
  64.  
  65. else if(s == 0){
  66. valid = false;
  67. }
  68.  
  69. if(!valid){
  70. valid = true;
  71. continue;
  72. }
  73.  
  74. centerY = r;
  75. centerX += r;
  76.  
  77. //print diamond
  78. printDiamond(r, n, s, centerX, centerY);
  79. centerX += r;
  80. }
  81. }
  82. public static function printDiamond(r:uint, n:uint, s:uint,
  83. centerX:uint, centerY:uint):void{
  84. var length:Number = r;
  85. var color:uint = COLOR_BLACK;
  86. for(var i:uint = 0; i < s; i++){
  87. printCircle(centerX, centerY, length, n, color);
  88. if(color == COLOR_BLACK)
  89. color = COLOR_WHITE;
  90. else
  91. color = COLOR_BLACK;
  92. length -= (r/s);
  93. }
  94. }
  95.  
  96. //liefert die x-Koordinate des Punktes, der sich vom Punkt (x,*)
  97. //aus gesehen im Abstand "radius" und Winkel "angle" befindet.
  98.  
  99. public static function printCircle(centerX:uint, centerY:uint,
  100. length:Number, n:uint, color:uint):void{
  101. var angle:Number = 0;
  102. var curCircleX:Number = xCoordinate(centerX, length, angle);
  103. var curCircleY:Number = yCoordinate(centerY, length, angle);
  104. var newCircleX:Number;
  105. var newCircleY:Number;
  106. while(angle < 2*Math.PI){
  107. angle += 2*Math.PI/n;
  108.  
  109. newCircleX = xCoordinate(centerX, length, angle);
  110. newCircleY = yCoordinate(centerY, length, angle);
  111.  
  112. //print a triangle
  113.  
  114. printTriangle(centerX, centerY, curCircleX, curCircleY,
  115. newCircleX, newCircleY, color);
  116.  
  117. curCircleX = newCircleX;
  118. curCircleY = newCircleY;
  119.  
  120. if(color == COLOR_BLACK)
  121. color = COLOR_WHITE;
  122. else
  123. color = COLOR_BLACK;
  124. }
  125. }
  126.  
  127. public static function printTriangle(centerX:uint, centerY:uint,
  128. curCircleX:Number, curCircleY:Number,
  129. newCircleX:Number, newCircleY:Number, color: uint):void{
  130. Drawing.graphics.lineStyle(1, COLOR_BLACK);
  131. Drawing.graphics.beginFill(color);
  132. Drawing.graphics.moveTo(centerX, centerY);
  133. Drawing.graphics.lineTo(curCircleX, curCircleY);
  134. Drawing.graphics.lineTo(newCircleX, newCircleY);
  135. Drawing.graphics.lineTo(centerX, centerY);
  136. Drawing.graphics.endFill();
  137. }
  138.  
  139. public static function xCoordinate (x:Number, r:Number, angle:Number): Number{
  140. return x+r*Math.cos(angle);
  141. }
  142.  
  143. //liefert die y-Koordinate des Punktes, der sich vom Punkt (*,y)
  144. //aus gesehen im Abstand "radius" und Winkel "angle" befindet.
  145.  
  146. public static function yCoordinate (y:Number, r:Number, angle:Number): Number{
  147. return y-r*Math.sin(angle);
  148. }
  149. }
  150. }
Add Comment
Please, Sign In to add comment