Projecting one vector onto another... why?

Sage
Posts: 1,403
Joined: 2005.07
Post: #16
Can you add the bit for Vperp, i dunno the formula.

Code:
// <applet code="ProjectingVectors.class" width="400" height="400"></applet>

import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.*;

public class ProjectingVectors extends java.applet.Applet {
    int dragMode;
    
    int nx, ny;
    int vx, vy;
    
    int scX, scY;
    
    Vect2D n, v;
    Vect2D vParr, vPerp;
    
    double nDv, nAbs, scale;
    
    public void init() {
        nx = (int)(Math.random()*400);
        ny = (int)(Math.random()*400);
        vx = (int)(Math.random()*400);
        vy = (int)(Math.random()*400);
        
        addMouseListener(new ML());
        addMouseMotionListener(new MML());
    }
    
    public void paint(Graphics g) {
        n = new Vect2D(nx-200, ny-200);
        n.mult(1.0/200.0);
        v = new Vect2D(vx-200, vy-200);
        v.mult(1.0/200.0);
        
        nDv = n.dot(v);
        nAbs = n.absSqrd();
        
        scale = nDv/nAbs;
        
        nAbs = Math.sqrt(nAbs);
        
        vParr = n.dup();
        vParr.mult(scale);
        scX = (int)(vParr.x*200+200);
        scY = (int)(vParr.y*200+200);
        
        g.setColor(Color.white);
        g.fillRect(0, 0, 400, 400);
        g.setColor(Color.black);
        g.drawLine(200, 200, nx, ny);
        g.drawLine(200, 200, vx, vy);
        g.setColor(Color.red);
        g.drawLine(200, 200, scX, scY);
        g.drawLine(200-1, 200, scX-1, scY);
        g.drawLine(200+1, 200, scX+1, scY);
        g.drawLine(200, 200-1, scX, scY-1);
        g.drawLine(200, 200+1, scX, scY+1);
        g.setColor(Color.black);
        g.fillOval(nx-2, ny-2, 4, 4);
        g.fillOval(vx-3, vy-3, 6, 6);
        
        g.drawString("N = ("+Double.toString(round(n.x))+", "+Double.toString(round(n.y))+")", nx+5, ny+5);
        g.drawString("V = ("+Double.toString(round(v.x))+", "+Double.toString(round(v.y))+")", vx+5, vy+5);
        g.drawString("N.V = ("+Double.toString(round(nDv))+")", 10, 20);
        g.drawString("|N| = ("+Double.toString(round(nAbs))+")", 10, 40);
    }
    
    public double round(double val) {
        int tmp = (int)(val*100);
        return (double)tmp/100.0;
    }
    
    class ML extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            dragMode = 0;
            if((Math.abs(e.getX()-nx) < 5) && (Math.abs(e.getY()-ny) < 5))
                dragMode = 1;
            else if((Math.abs(e.getX()-vx) < 5) && (Math.abs(e.getY()-vy) < 5))
                dragMode = 2;
        }
    }
    
    class MML extends MouseMotionAdapter {
        public void mouseDragged(MouseEvent e) {
            if(dragMode == 1) {
                nx = e.getX();
                ny = e.getY();
                repaint();
            }
            else if(dragMode == 2) {
                vx = e.getX();
                vy = e.getY();
                repaint();
            }
        }
    }
}

class Vect2D {
    double x, y;
    
    public Vect2D(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public Vect2D dup() {
        return new Vect2D(x, y);
    }
    
    public void mult(double n) {
        x *= n;
        y *= n;
    }
    
    public double absSqrd() {
        return x*x+y*y;
    }
    
    public double dot(Vect2D b) {
        return x*b.x+y*b.y;
    }
}

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 691
Joined: 2002.04
Post: #17
Thanks for the help everybody Wink With everybody's explanations I'm a lttle closer to understanding why the fornula might be usefui, and I was, at last, able to figure out where I was going wrong to get such odd results (which was caused by an error in an area in no way related to the formula Wacko)

@unknown: if I understand you correctly: Vperpendicular = V - Vparallel

Mark Bishop
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #18
Ok, its just mazilurik seemed to calculate it some way like this (2, 3) * ((3, 1).(2, 3))/|(2, 3)|^2 and i couldnt see (2, 3) in the diagram anywhere.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 57
Joined: 2002.04
Post: #19
(2, 3) is the vector perpendicular to n; I found the "component" of v perpendicular to n by finding the "component" parallel to a vector perpendicular to n. If you're already finding v-parallel, it's easier to just subtract as Sealfin said.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #20
by component do you mean length
Itd be good finding the fasted way of calculating them independently because you probably wont need both.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 57
Joined: 2002.04
Post: #21
unknown Wrote:by component do you mean length

Yes; I used "component" (in quotes; not a technically proper usage of the term) because I like to think of projections in the same sense as the x and y components of the vector, except based on an arbitrary axis.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #22
Yes, i know exactly what you mean.
The other "component" is 0

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Post Reply