Sneha Bansal
Sneha Bansal

Reputation: 941

Find out the coordinates using slope and start point

I am trying to achieve a task which is to resize the line after taking length input from user.So what I have currently is old length,old points(P1 ,P2) and have to find the new P2 after taking new length from user.I am here pasting code which I have tried to resize line using slope but its not working.I am totally new in canvas part to any kind of search hint will also be appreciable.Thanks in advance.

Method to find out the angle between previous points:

public double calculateAngle(){
  if(selectedShape!=null){      
  InnWall shape  = (InnWall)selectedShape;
    if(shape!=null)
    return calAngle(shape.y2-shape.y1, shape.x2-shape.x1);
  }
return -1;
}

Method to find out he coordinates

public void calCoordinates(double length){

        InnWall shape  = (InnWall)selectedShape;
        if(shape!=null){
            double angle = calculateAngle();
        Log.e(TAG, "cal angle"+(int)calculateAngle());
        Log.e(TAG, "cal length"+(int)length);
        Log.e(TAG, "x coodinatee"+shape.x1+length*Math.cos(angle));
        Log.e(TAG, "y coodinatee"+shape.y1+length*Math.sin(angle));
        shape.x2=(float)(shape.x1+length*Math.cos(angle));
        shape.y2=(float)(shape.y1+length*Math.sin(angle));

}

private  double calAngle(double dy,double dx){
        return double compassBearing=dy/dx;
}

Upvotes: 0

Views: 101

Answers (1)

yonutix
yonutix

Reputation: 2439

double scale = old_length/new_length;
dx = p2.x - p1.x
dy = p2.y - p1.y

You know that:

 - scale_x^2 + scale_y^2 = scale^2
 - scale_x/scale_y = dx/dy

Thus:

scale_y = sqrt(scale * scale * dy *dy/(dx*dx + dy*dy));
scale_x = scale_y*dx/dy;

If the relative point is P1 Then p2.x += (p2.x-p1.x) * scale_x p2.y += (p2.y-p1.y) * scale_y

In theory, for scaling you need to multiply the points with a scaling matrix which in your case is 2D.

[ 1  0  dx ] [ p1.x ]    [ p2.x ]
[ 0  1  dy ] [ p1.y ]  = [ p2.y ]
[ 0  0  1  ] [ 1    ]    [ 1  ]

Upvotes: 1

Related Questions