2016-03-26 13 views
2

İşleme çizimimde bir çizgi segmentim ve bir dairem var. Çemberin merkezini, nokta q, en yakın noktayı bulmak için, p, çizgi segmentinde ve daireye doğru hareket etmesini istiyorum. Bunu nasıl kodlayacağımı tam olarak emin değilim (işlenirken), bu nedenle herhangi bir öneri harika olurdu! Teşekkürler! Bu defa kodudur:Bir noktanın işlem sırasında bir çizginin ortogonal projeksiyonu?

int xPos1 = 200; 
int yPos1 = 200; 
int xp1 = 50; 
int yp1 = 50; 
int xp2 = 350; 
int yp2 = 50; 

void setup() { 
    size(400, 400); 
    strokeWeight(2); 
    line(xp1, yp1, xp2, yp2); 
    strokeWeight(1); 
} 

void draw() { 
    drawCircle(); 
} 

void drawCircle() { 
    fill(50, 120, 120); 
    //circle 
    ellipse(xPos1, yPos1, 75, 75); 
    //circle center 
    ellipse(xPos1, yPos1, 7, 7); 
    fill(255); 
    text("Q", xPos1 + 15, yPos1 + 5); 
    fill(50, 120, 120); 
} 

cevap

2

aşağıdaki gibi bir hat üzerine bir nokta izdüşümüdür:

Başlangıç ​​bir şekilde, x = a + t * n bir hat ve bir noktaya p. (- n) p, dot (a -) n çok

Elimizdeki (- p)

:


noktası p hattı üzerinde en yakın noktası temsil eden vektör compontent olan (- n) p, dot (a -) n-


a: p + (a - p) - sadeleştirme sonra - ((p) n-nokta) n

Elimizdeki

Not: ((a - p) dot n) n, satır boyunca en yakın noktadan başlayarak (örn. en yakın noktadan p'ye kadar a)

Hayatı biraz daha kolaylaştırmak için PVector s kullanalım.

PVector p = new PVector(200, 200); 
PVector a = new PVector(50, 50); 
PVector b = new PVector(350, 50); 
PVector n = new PVector(350, 50); // |p2 - p1| 

void setup() { 
    size(400, 400); 
    strokeWeight(2); 
    strokeWeight(1); 

    // initialize our normalized (unit length) line direction 
    n.sub(a); 
    n.normalize(); 
} 

void draw() { 
    drawCircle(); 
} 

PVector getNearestPointOnLine(PVector p, PVector a, PVector n){ 
    // the notation turns the computation inside out, 
    // but this is equivalent to the above equation 
    PVector q = PVector.mult(n, -PVector.sub(a, p).dot(n)); 
    q.add(a); 
    return q; 
} 

void drawCircle() { 
    // lets draw everything here where we can see it 
    background(255, 255, 255); 
    line(a.x, a.y, b.x, b.y); 

    fill(50, 120, 120); 
    //circle 

    // NOTE: this may require hooking up a mouse move event handler 
    p.x = mouseX; 
    p.y = mouseY; 
    PVector q = getNearestPointOnLine(p, a, n); 

    ellipse(q.x, q.y, 75, 75); 
    //circle center 
    ellipse(q.x, q.y, 7, 7); 
    fill(0); // make text visible on white background 
    text("Q", q.x + 15, q.y + 5); 
    //fill(50, 120, 120); 
} 

Referans: noktası q yine çizgi parçasının her iki ucundan ileriye görüneceğini https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation

+0

Not bir "sınır", burada uygulanan kontroller vardır, yani. – Nolo

+0

Teşekkürler! Bu çok yardımcı oldu. –

+0

Aradığım bir şey de, en yakın nokta çizgi segmentinde olana doğru hareket eden dairedir. Çember hareketini gördüğümüz gibi. Bunun nasıl yapılacağı hakkındaki düşünceler harika olurdu! –

İlgili konular