Vector2DrawLine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Extends line type with methods for Vector2 and Segment2.
library('Vector2DrawLine')



//#region Imports:

import RicardoSantos/CommonTypesMath/1 as TMath
import RicardoSantos/Vector2/1 as Vector2

//#endregion



//#region ~~~ Constructor:

// new       () {

// @function Draws a line using Segment type to hold its coordinate properties..
// @param origin Vector2 . Origin vector of the line.
// @param target Vector2 . Target vector of the line.
// @param xloc   string
// @param extend string
// @param color  color
// @param style  string
// @param width  int
// @returns line object
export new (TMath.Vector2 origin, TMath.Vector2 target, string xloc = 'bi', string extend = 'n', color color = #2196f3, string style = 'sol', int width = 1) =>
    line.new(
     x1     = int(origin.x), 
     y1     = origin.y, 
     x2     = int(target.x), 
     y2     = target.y, 
     xloc   = xloc, 
     extend = extend, 
     color  = color, 
     style  = style, 
     width  = width
     )

// @function Draws a line using Segment type to hold its coordinate properties..
// @param segment Segment2 . Segment with positional coordinates.
// @param xloc    string
// @param extend  string
// @param color   color
// @param style   string
// @param width   int
// @returns line object
export new (TMath.Segment2 segment, string xloc = 'bi', string extend = 'n', color color = #2196f3, string style = 'sol', int width = 1) =>
    line.new(
     x1     = int(segment.origin.x), 
     y1     = segment.origin.y, 
     x2     = int(segment.target.x), 
     y2     = segment.target.y, 
     xloc   = xloc, 
     extend = extend, 
     color  = color, 
     style  = style, 
     width  = width
     )

// }

//#endregion



//#region ~~~ Methods:

// rotate_around () {

// @function Instance method to rotate line around center vector (modifies input line).
// @param this   line    . Line object.
// @param center Vector2 . Center of rotation.
// @param angle  float   . Rotation angle in degrees.
// @returns line. Rotated line object.
export method rotate_around (line this, TMath.Vector2 center, float angle) =>
    TMath.Vector2 _a = Vector2.rotate_around(Vector2.new(this.get_x1(), this.get_y1()), center, angle)
    TMath.Vector2 _b = Vector2.rotate_around(Vector2.new(this.get_x2(), this.get_y2()), center, angle)
    this.set_xy1(int(_a.x), _a.y)
    this.set_xy2(int(_b.x), _b.y)
    this

// }

//#endregion


rotation = input.float(0.0, step=5.0)
a = Vector2.new(bar_index+0.0, 0.0)
b = Vector2.new(bar_index+10.0, 10.0)
c = Vector2.new(bar_index+0.0, 10.0)
s = TMath.Segment2.new(a, b)
if barstate.islast
    la = new(s)
    lb = new(b, c)
    lb.rotate_around(b, rotation)