检测线的交叉点

时间:2014-03-31 08:41:10

标签: java arrays processing trigonometry intersection

我正在尝试解决这个问题:在屏幕上绘制随机线,然后在所有线的交叉处创建点。我的解决方案:我使用小点来构造线条,而不是创建实线。每个“行”都是一个名为Pointe的类的实例。我将每个线条实例的所有坐标存储在它自己的数组中。为了检测交集,我写了函数collide()来比较每一行的存储坐标。如果距离<10,我将线的厚度更改为10像素,以创建“气球”效果。

int p = 0;
class Pointe {
  int x;
  int y;
  int speedX;
  int speedY;
  float size = 2;
  color c = color(random(255),random(255),random(255));
  int position_stored[] = {};
  Pointe(int xPosition, int yPosition, int speed_X, int speed_Y) {
    x = xPosition;
    y= yPosition;
    speedX = int(random(speed_X));
    speedY = int(random(speed_Y));
  }

  int b = int(mouseX);
  int n = int(mouseY);

  void move() {
    x = x + speedX;
    y = y + speedY;
    if (x>60) {x = x; y = y;}
    if (y>60) {y = y; x = x;}
    store_position();
  }

  void display() {
    noStroke();
    fill(c);
    ellipse(x,y,size,size);
  }
  int getX() {return x;}
  int getY() {return y;}
  void store_position() {
    int position_stored2[] = append(position_stored,getX());
    int position_stored3[] = append(position_stored2,getY());
    position_stored = position_stored3;
  }

  void collide(int[] a) {
    for (int i = 1; i < a.length ; i=i+2) {
        int distance = int(dist(x,y,a[i-1],a[i])); 
        if (distance < 20) {size = 10;}
        else {size = 2;}
    }
  }
}
int number = 109;
void setup(){
  size(600,600);
  background(255);
  for (int i = 0; i <number; i++) {
    points[i] = new Pointe(int(random(600)),int(random(600)),int(random(-6,6)),int(random(-6,6)));
  }
}

Pointe[] points =  new Pointe[number];

void draw(){
  for (int i = 0; i <number; i++) {
    for (int z = 0; z <number; z++) {
      if (i == z) {}
      points[i].collide(points[z].position_stored);
    }
    points[i].display();
    points[i].move();   
  }
}

2 个答案:

答案 0 :(得分:2)

这是一个简单的数学问题。你对每一行所拥有的是平面中的2个点,你可以使用这两个点得到线方程式https://www.khanacademy.org/test-prep/algebra1-brushup/graph-eqns-brushups/v/equation-of-a-line-3 如果您有两条不同线的等式,您可以通过G(x)= F(x)找到它们相交的点。

此致

答案 1 :(得分:0)

我目前无法在此处使用处理脚本。处理与Java非常相似,只需稍作修改,这样的脚本就可以作为Java Applet运行。

但是,您的方法在很多方面都值得怀疑。

draw方法中的第一个调用实际应该是

void draw()
{
    background(0xFFFFFF);
    ...
}

为了清除背景。如果你不这样做,那么你看到的任何东西都与你实际拥有的“状态”无关。

目前,您正在position_stored数组中存储点的坐标(移动后)。但是这个数组的内容不是用于绘画,而只是用于距离计算!

最重要的是:这个动画应该运行多长时间?它运行的时间越长,您需要的内存就越多。你总是存储新的点坐标......

所以最后,在最好的情况下,每个人都可以猜测你想要实现的目标。而我猜测就是这样的,但考虑到我对处理的了解有限,并且打算至少保持与你最初发布的轻微的相似性,这仍然是很差已实施,应该以不同的方式解决。

import processing.core.PApplet;

public class ScribblingTest extends PApplet
{
    class Pointe
    {
        int x;
        int y;
        int speedX;
        int speedY;
        float size = 2;
        int c = color(random(255), random(255), random(255));

        Pointe(int xPosition, int yPosition, int speed_X, int speed_Y)
        {
            x = xPosition;
            y = yPosition;
            speedX = (int)(random(speed_X));
            speedY = (int)(random(speed_Y));
        }

        Pointe(Pointe other)
        {
            x = other.x;
            y = other.y;
            speedX = other.speedX;
            speedY = other.speedY;
            c = other.c;
        }


        void move()
        {
            x = x + speedX;
            y = y + speedY;
            if (x > 600)
            {
                x = 0;
            }
            if (y > 600)
            {
                y = 0;
            }
            if (x < 0)
            {
                x = 600;
            }
            if (y < 0)
            {
                y = 600;
            }
        }

        void display()
        {
            noStroke();
            fill(c);
            ellipse(x, y, size, size);
        }

        int getX()
        {
            return x;
        }

        int getY()
        {
            return y;
        }

        boolean collide(int[] a)
        {
            for (int i = 1; i < a.length; i = i + 2)
            {
                int distance = (int)(dist(x, y, a[i - 1], a[i]));
                if (distance < 20)
                {
                    return true;
                }
            }
            return false;
        }
    }

    int number = 20;
    int maxSteps = 300;
    int currentStep = 0;
    Pointe[][] points = new Pointe[number][maxSteps];

    public void setup()
    {
        size(600, 600);
        background(255);
        for (int i = 0; i < number; i++)
        {
            points[i] = new Pointe[maxSteps];
            points[i][0] = new Pointe(
                (int)(random(600)), 
                (int)(random(600)), 
                (int)(random(-6, 6)), 
                (int)(random(-6, 6)));
        }
    }


    public void draw()
    {
        background(0xFFFFFF);

        int currentIndex = currentStep % maxSteps;
        int nextIndex = (currentStep + 1) % maxSteps;
        for (int i = 0; i < number; i++)
        {
            Pointe currentP = points[i][currentIndex];
            Pointe nextP = new Pointe(currentP);
            nextP.move();

            boolean collides = false;
            for (int j=0; j<number; j++)
            {
                if (i != j)
                {
                    for (int k=0; k<maxSteps; k++)
                    {
                        Pointe otherP = points[j][k];
                        if (otherP != null)
                        {
                            float d = dist(nextP.x, nextP.y, otherP.x, otherP.y);
                            if (d < 20)
                            {
                                collides = true;
                                break;
                            }
                        }
                    }
                    if (collides)
                    {
                        break;
                    }
                }
            }
            if (collides)
            {
                nextP.size = 10;
            }
            else
            {
                nextP.size = 2;
            }

            points[i][nextIndex] = nextP;
        }

        for (int i = 0; i < number; i++)
        {
            for (int j=0; j<maxSteps; j++)
            {
                Pointe p = points[i][j];
                if (p != null)
                {
                    p.display();
                }
            }
        }
        currentStep++;


    }

}