5587浏览
查看: 5587|回复: 0

[项目] Processing系列 光与镜子迷宫

[复制链接]


代码如下:
改变setup中polygon生成函数变成你想要的迷宫,话说可以用这玩意来击败海军大将黄猿么?
  1. void setup() {
  2.   size(300, 300);
  3.   background(55);
  4.   stroke(255);
  5.   strokeWeight(1);
  6.   Polygon[] polygons = new Polygon[4];
  7.   polygons[0] = new Polygon(new Point[] {
  8.     new Point(50, 30),
  9.     new Point(20, 120),
  10.     new Point(30, 270),
  11.     new Point(230, 280),
  12.     new Point(280, 170),
  13.     new Point(250, 20)
  14.   });
  15.   polygons[1] = new Polygon(new Point[] {
  16.     new Point(70, 80),
  17.     new Point(120, 75),
  18.     new Point(140, 120),
  19.     new Point(90, 160),
  20.     new Point(50, 130)
  21.   });
  22.   polygons[2] = new Polygon(new Point[] {
  23.     new Point(60, 210),
  24.     new Point(120, 190),
  25.     new Point(110, 240),
  26.     new Point(80, 230),
  27.   });
  28.   polygons[3] = new Polygon(new Point[] {
  29.     new Point(200, 160),
  30.     new Point(230, 180),
  31.     new Point(180, 200),
  32.   });
  33.   for (int i=0; i<polygons.length; i++) {
  34.     polygons[i].display();
  35.   }
  36.   Point point = new Point(200, 100);
  37.   Light light = new Light(point, PVector.random2D());
  38.   strokeWeight(4);
  39.   point(point.x, point.y);
  40.   for (int j=0; j<10; j++) {
  41.     for (int i=0; i<polygons.length; i++) {
  42.       check(light, polygons[i]);
  43.     }
  44.     strokeWeight(2.0/(j+1));
  45.     light.display();
  46.     float x = light.p.x+light.v.x*light.t;
  47.     float y = light.p.y+light.v.y*light.t;
  48.     PVector v = PVector.sub(light.v, PVector.mult(light.ln, 2*PVector.dot(light.v, light.ln)));
  49.     light = new Light(new Point(x, y), v);
  50.   }
  51. }
  52. void check(Light light, Polygon polygon) {
  53.   for (int i=0; i<polygon.lines.length; i++) {
  54.     Line line = polygon.lines[i];
  55.     PVector v = new PVector(line.p.x-light.p.x, line.p.y-light.p.y);
  56.     float location = PVector.dot(v, line.n);
  57.     float direction = PVector.dot(light.v, line.n);
  58.     if (direction<0) {
  59.       float t = location/direction;
  60.       float u = -PVector.dot(v, light.n)/PVector.dot(line.v, light.n);
  61.       if (location<=0 && t<light.t && u>=0 && u<=1) {
  62.         light.t = t;
  63.         light.ln = line.n;
  64.       }
  65.     }
  66.   }
  67. }
  68. class Polygon {
  69.   Line[] lines;
  70.   Polygon(Point[] points) {
  71.     lines = new Line[points.length];
  72.     for (int i=1; i<lines.length; i++) {
  73.       lines[i-1] = new Line(points[i-1].x, points[i-1].y, points[i].x, points[i].y);
  74.     }
  75.     lines[lines.length-1] = new Line(points[points.length-1].x, points[points.length-1].y, points[0].x, points[0].y);
  76.   }
  77.   void display() {
  78.     for (int i=0; i<lines.length; i++) {
  79.       lines[i].display();
  80.     }
  81.   }
  82. }
  83. class Light {
  84.   float t=1000;
  85.   Point p;
  86.   PVector v, n, ln;
  87.   Light(Point p, PVector v) {
  88.     this.p = p;
  89.     this.v = v;
  90.     this.v.normalize();
  91.     n = new PVector(v.y, -v.x);
  92.     n.normalize();
  93.   }
  94.   void display() {
  95.     line(p.x, p.y, p.x+v.x*t, p.y+v.y*t);
  96.   }
  97. }
  98. class Line {
  99.   Point p;
  100.   PVector v, n;
  101.   Line(float x0, float y0, float x1, float y1) {
  102.     p = new Point(x0, y0);
  103.     v = new PVector(x1-x0, y1-y0);
  104.     n = new PVector(y1-y0, x0-x1);
  105.     n.normalize();
  106.   }
  107.   void display() {
  108.     line(p.x, p.y, p.x+v.x, p.y+v.y);
  109.   }
  110. }
  111. class Point {
  112.   float x, y;
  113.   Point(float x, float y) {
  114.     this.x = x;
  115.     this.y = y;
  116.   }
  117. }
复制代码

授权转载自 任远媒体实验室
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail