Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added drawLineVector and drawArrowVector #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions application1/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,37 @@ var vector = {
divideBy: function(val) {
this._x /= val;
this._y /= val;
},

drawLineVector: function(context,x,y,scale,color,thickness){ /* This function draws a line vector, whose length is proportional to its magnitude of the invoking vector */
var unit_vector = vector.create(1,0) /* It Takes canvas context, x,y coordinates, scale, color and thickness as inputs */
unit_vector.setAngle(this.getAngle()); /* Git Username: ckiran2508 */
unit_vector.setX(scale*this.getX());
unit_vector.setY(scale*this.getY());
let head_x=x+unit_vector.getX()
let head_y=y+unit_vector.getY()
context.beginPath();
context.moveTo(x,y);
context.lineTo(head_x,head_y);
context.strokeStyle = color;
context.lineWidth=thickness;
context.stroke();
context.closePath();
context.strokeStyle = "black";
},

drawArrowVector: function(context,x,y,scale,color,thickness){ /* This function draws a line vector with arrow head at its tip, whose length is proportional to its magnitude of the invoking vector */
if(this.getLength() > 0){ /* It Takes canvas context, x,y coordinates, scale, color and thickness as inputs */
this.drawLineVector(context,x,y,scale,color,thickness)
let head_x = x+ (this.getX() * scale)
let head_y = y+ (this.getY() * scale)
let top_vector = vector.create(7,0)
top_vector.setAngle(this.getAngle() - 2.35)
let bottom_vector = vector.create(7,0)
bottom_vector.setAngle(this.getAngle() + 2.35)
top_vector.drawLineVector(context,head_x,head_y,1,color,thickness)
bottom_vector.drawLineVector(context,head_x,head_y,1,color,thickness)
}
}

};