检测线的交叉点 [英] Detecting line's intersections

查看:121
本文介绍了检测线的交叉点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来解决这个问题:在屏幕上画线随机然后在所有线路的交叉点创建。我的解决方案:而不是创建实线,我用小点来构造线。每一行是一个称为足尖类的一个实例。我每行实例的所有坐标存储在它自己的数组。为了检测路口,我写了碰撞功能(),以每行的存储坐标进行比较。如果距离< 10,我改线的厚度10像素打造的气球效应

  INT P = 0;
类足尖{
  INT X;
  诠释Ÿ;
  INT speedX;
  INT迅速;
  浮动的大小= 2;
  颜色为c =颜色(随机(255),随机(255),随机(255));
  INT position_stored [] = {};
  潘特(INT xPosition位置,诠释yPosition,诠释speed_X,诠释speed_Y){
    X = xPosition位置;
    Y = yPosition;
    speedX = INT(随机(speed_X));
    SPEEDY = INT(随机(speed_Y));
  }  INT B = INT(mouseX);
  INT N = INT(mouseY的);  无效移动(){
    X = X + speedX;
    Y = Y +快速;
    如果(X→60){X = X; Y = Y;}
    如果(γ→60){Y = Y; X = X;}
    store_position();
  }  无效显示(){
    noStroke();
    填写(C);
    椭圆(X,Y,尺寸,大小);
  }
  INT的getX(){返回X;}
  诠释的getY(){返回Ÿ;}
  无效store_position(){
    INT position_stored2 [] =追加(position_stored,的getX());
    INT position_stored3 [] =追加(position_stored2,的getY());
    position_stored = position_stored3;
  }  无效碰撞(INT [] A){
    的for(int i = 1; I<则为a.length; I = I + 2){
        INT距离= INT(DIST(X,Y,一个[I-1]中,[I]));
        如果(距离< 20){大小= 10;}
        其他{大小= 2;}
    }
  }
}
INT数= 109;
无效设置(){
  大小(600600);
  背景(255);
  的for(int i = 0; I<数;我++){
    点[I] =新潘特(INT(随机(600)),INT(随机(600)),INT(随机(-6,6)),INT(随机(-6,6)));
  }
}潘特[]积分=新足尖[数字];无效的draw(){
  的for(int i = 0; I<数;我++){
    对(INT z = 0的; z,其中;号; z ++){
      如果(我== Z){}
      点[I] .collide(分[Z] .position_stored);
    }
    点[I]。显示();
    点[I] .move();
  }
}


解决方案

这是一个简单的数学问题。你有什么对每一行都是在飞机上2个点,可以使用这些2点按照此<一个得到直线方程href=\"https://www.khanacademy.org/test-$p$pp/algebra1-brushup/graph-eqns-brushups/v/equation-of-a-line-3\" rel=\"nofollow\">https://www.khanacademy.org/test-$p$pp/algebra1-brushup/graph-eqns-brushups/v/equation-of-a-line-3
那些有2个不同的线方程可以找到他们由G(x)的= F(x)的

相交的点

问候

I am trying to solve this problem: drawing random lines on the screen then create dots at intersection of all the lines. My solution: instead of creating real lines, I use little dots to construct lines. Each "line" is an instance of a class called Pointe. I store all the coordinates of each line instance in it's own array. In order to detect intersection, I wrote function collide() to compare each line's stored coordinates. If the distance <10, I change the line's thickness to 10 pixel to create the "balloon" effect.

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();   
  }
}

解决方案

This is a simple math problem. What you have for every line are 2 points in the plane, you can get the line equation using those 2 points follow this https://www.khanacademy.org/test-prep/algebra1-brushup/graph-eqns-brushups/v/equation-of-a-line-3 Ones you have the equation of 2 different lines you can find the points where they intersect by G(x) = F(x).

Regards

这篇关于检测线的交叉点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆