Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
Detect interception with line

How can I make a algorithm that detects if a point (x, y) intercepts with a line. (x1, y1, x2, y2)?

I have already tried :

boolean onLine(float a, float b, float c, float d, float x, float y){
    boolean answer = false;
    float[] p1 = new float[] {a, b};
    float[] p2 = new float[] {c, d};
    float x_spacing = (p2[0] - p1[0]) / ((a+c)/2 + (b+d));
    float y_spacing = (p2[1] - p1[1]) / ((a+c)/2 + (b+d));
    List<float[]> line = new ArrayList();
    float currentX = 0;
    float currentY = 0;
    while(currentX+a<c&&currentY+b<d){
        currentX += x_spacing;
        currentY += y_spacing;
        line.add(new float[]{a+currentX, b+currentY});
    }
    for(int j = 0; j < line.size(); j++){
        if(x > line.get(j)[0]-x_spacing && x < line.get(j)[0]+x_spacing && y > line.get(j)[1]- 
        y_spacing && y < line.get(j)[1]+y_spacing){
            answer = true;
            println("Hit line!");
            break;
         }
    }
return answer;

}

This works sometimes, but is not always consistent.

I am putting this with a physics game, and I need this so the ball can roll down a line.

What are some ways I can improve it so that it works?.

EDIT: Thanks to Felix Castor I got it working. Here is the final Code:

boolean onLine(float x1, float y1, float x2, float y2, float xt, float yt, 
               float wid, float hit){
    float Y = (y2 - y1)/(x2 - x1)* xt + y1 -(y2 - y1)/(x2 - x1) * x1;
    boolean answer = false;
    if(abs(Y - yt) < 5) answer = true;
    if(abs(Y - yt-hit) < 5) answer = true;
    if(abs(Y - yt-(hit/2)) < 5) answer = true;
    if(abs(Y - yt+hit) < 5) answer = true;
    if(abs(Y - yt+(hit/2)) < 5) answer = true;
    return answer;

}

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Using slope intercept form you can plug in your x and see if the y's are equal.

y = m*x + b

m = (y2 - y1)/(x2 - x1)

b = y1 - (y2 - y1)/(x2 - x1) * x1

So the equation becomes

Y = (y2 - y1)/(x2 - x1)* X + y1 -(y2 - y1)/(x2 - x1) * x1

given a point (xt, yt) you can plug in the xt into X and evaluate then compare the result to yt. If they are equal then the point is on the line.

if Y == yt given xt then the point is on the line.

You will need to handle the case where you have strictly horizontal lines as edge cases. Those will blow up the equation.

Edit: Conditions Changed

Since you are wanting to determine how far from the line a point is I would say the formula for the distance between a point and a line in cartesian space would be the way to go. See Distance from a point to a line section Line Defined By Two Points. The formula looks ugly but it's straight forward.

enter image description here

  double numerator = Math.abs((y2 - y1) * xt - (x2 - x1) * yt + x2 * y1 - y2 * x1);
  double denominator = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
  double distance = numerator / denominator;

As before your test point is (xt, yt) and your line is defined by two points (x1, y1) and (x2, y2). Because distance is always >= 0 your test would be:

if( distance <= tolerance) return true

I think this is a better approach if you are interested in a tolerance.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda