Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. package lighting;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Polygon;
  6. import java.awt.geom.Line2D;
  7. import java.awt.geom.Point2D;
  8. import java.awt.geom.Rectangle2D;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11.  
  12. import javax.swing.JPanel;
  13.  
  14. public class Screen extends JPanel {
  15.  
  16. private Graphics g;
  17.  
  18. private int w;
  19. private int h;
  20. private int count = 0;
  21.  
  22. private ArrayList<Ray> rays;
  23.  
  24. public Screen(int w, int h) {
  25. this.w = w;
  26. this.h = h;
  27. this.rays = new ArrayList<Ray>();
  28. repaint();
  29. }
  30.  
  31. public void paint(Graphics g) {
  32. this.g = g;
  33. paint();
  34. }
  35.  
  36. public void paint() {
  37. count++;
  38. g.setColor(Color.getHSBColor(1f, 0f, 1f));
  39. g.fillRect(0, 0, w, h);
  40. rays.clear();
  41. renderObsc();
  42. renderLines();
  43. }
  44. private void renderObsc() {
  45. for(Obscurer o : Main.obscList) {
  46. g.setColor(Color.DARK_GRAY);
  47. Polygon profile = o.getProfile();
  48. g.fillPolygon(profile);
  49. }
  50. }
  51.  
  52. private void renderLines() {
  53. for(Obscurer o : Main.obscList) {
  54. Line2D.Double[] segments = o.lineSegments();
  55. for(Line2D.Double segment : segments) {
  56.  
  57. g.setColor(Color.MAGENTA);
  58. g.drawLine((int)segment.getX1(), (int)segment.getY1(), (int)segment.getX2(), (int)segment.getY2());
  59.  
  60. calculateLines(segment);
  61. }
  62. }
  63.  
  64. double[][] screenEdgePoints = new double[][]
  65. {{0,0},{Main.screenWidth,0},{Main.screenWidth,Main.screenHeight},{0,Main.screenHeight}};
  66. for(double[] point : screenEdgePoints) {
  67. double dx = point[0]-Main.light.position.getX();
  68. double dy = point[1]-Main.light.position.getY();
  69. double angle = Math.atan2(dy, dx);
  70. angle = (angle+Math.PI*6)%(Math.PI*2);
  71. calculateLine(angle);
  72. }
  73.  
  74.  
  75. Collections.sort(rays, new CustomComparator());
  76. Polygon p = new Polygon();
  77. for(int i = 0; i < rays.size(); i++) {
  78. Ray ray0 = rays.get(i);
  79. double[] endpoint0 = new double[] {Main.light.position.getX()+(Math.cos(ray0.getRad())*ray0.getT2()),Main.light.position.getY()+(Math.sin(ray0.getRad())*ray0.getT2())};
  80. p.addPoint((int)endpoint0[0], (int)endpoint0[1]);
  81. }
  82. g.setColor(Color.getHSBColor(1, .5f, 0.85f));
  83. g.fillPolygon(p);
  84. }
  85.  
  86. private void calculateLines(Line2D.Double segment) {
  87. double ldx = segment.getX1()-Main.light.position.getX();
  88. double ldy = segment.getY1()-Main.light.position.getY();
  89. double lightAngle = Math.atan2(ldy, ldx);
  90. calculateLine(lightAngle-0.00001);
  91. calculateLine(lightAngle);
  92. calculateLine(lightAngle+0.00001);
  93.  
  94. }
  95. private void calculateLine(double rad) {
  96. rad = (rad+=6*Math.PI)%(2*Math.PI);
  97. double cos = Math.cos(rad);
  98. double sin = Math.sin(rad);
  99. double[] p = new double[] {Main.light.position.getX(),Main.light.position.getY()};
  100. double[] r = new double[] {cos,sin};
  101.  
  102. double lowest_t2 = Main.light.radius;
  103.  
  104. for(Obscurer o : Main.obscList) {
  105. Line2D.Double[] segments = o.lineSegments();
  106. for(Line2D.Double seg : segments) {
  107. double dy = seg.getY2()-seg.getY1();
  108. double dx = seg.getX2()-seg.getX1();
  109. double[] q = new double[] {seg.getX1(),seg.getY1()};
  110. double[] s = new double[] {dx,dy};
  111.  
  112. double r_cross_s = cross(r,s);
  113. double[] q_minus_p = {q[0]-p[0],q[1]-p[1]};
  114. double q_minus_p__cross_s = cross(q_minus_p,s);
  115. double q_minus_p__cross_r = cross(q_minus_p,r);
  116.  
  117. double u2 = q_minus_p__cross_r/r_cross_s;
  118. double t2 = q_minus_p__cross_s/r_cross_s;
  119.  
  120. if(u2 >= 0 && u2 <= 1 && t2 >= 0 && t2 <= Main.light.radius) {
  121. if(t2<lowest_t2) {
  122. lowest_t2 = t2;
  123. }
  124.  
  125. }
  126. }
  127. }
  128. rays.add(new Ray(rad,lowest_t2));
  129. }
  130. private double cross(double[] first, double[] second) {
  131. return first[0]*second[1] - first[1]*second[0];
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement