참조하십시오. 다른 두 개의 정수 변수 인 x
과 y
이 있습니다.
이 새로운 지점 (x, y)
이이 줄에 있는지 확인하고 싶습니다. 나는 다음과 같은 스레드에서 찾고있다 :
Given a start and end point, and a distance, calculate a point along a line
내가 함께 왔어요 다음
if(x >= x1 && x <= x2 && (y >= y1 && y <= y2 || y <= y1 && y >= y2))
{
float vx = x2 - x1;
float vy = y2 - y1;
float mag = sqrt(vx*vx + vy*vy);
// need to get the unit vector (direction)
float dvx = vx/mag; // this would be the unit vector (direction) x for the line
float dvy = vy/mag; // this would be the unit vector (direction) y for the line
float vcx = x - x1;
float vcy = y - y1;
float magc = sqrt(vcx*vcx + vcy*vcy);
// need to get the unit vector (direction)
float dvcx = vcx/magc; // this would be the unit vector (direction) x for the point
float dvcy = vcy/magc; // this would be the unit vector (direction) y for the point
// I was thinking of comparing the direction of the two vectors, if they are the same then the point must lie on the line?
if(dvcx == dvx && dvcy == dvy)
{
// the point is on the line!
}
}
이 작동하지 않는 것, 또는이 아이디어는 구타입니까?
2 차원에서 (x1, y1) 및 (x2, y2)에서 얻은 선 방정식에 (x, y)를 단순히 두지 않는 이유는 무엇입니까? – P0W
'(x, y)'는 (제목에서 알 수 있듯이) 점 (벡터가 질문을 나타내는 것처럼 보입니다)입니까? 질문이 실제로 의미가있는 것처럼 보일 뿐이지 만 두 벡터에 의해 정의 된 선이 교차하는지 여부를 말하는 것일 수 있습니다. –
먼저, 귀하의 조건/가정을 기재하십시오. 부동 소수점을 처리하는 경우 부동 소수점 부정확성을 고려해야합니다. 수식으로의 직접적인 수학은 효과가 없습니다. – dornhege