diff --git a/application1/vector.js b/application1/vector.js index 4d66796..d1cf894 100644 --- a/application1/vector.js +++ b/application1/vector.js @@ -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) + } } + }; \ No newline at end of file