function [] = draw_vec(start_vec,vector,varargin)
%draw_vec(start_vec, vector, 'Name', Value, ...)
% This function draws a vector with various options.
%
% Required parameters:
% start_vec = Starting position of the phasor
% vector = Phasor
%
% Optional parameters (Name-Value pairs):
% 'text_string'  = Label of the vector (default: '')
% 'arrowscale'   = Scaling of the arrowhead. It corresponds to the height of an isosceles triangle (default: 0.15)
% 'double_arrow' = Arrowhead at both ends (default: false)
% 'no_arrow'     = No arrowhead (default: false)
% 'text_size'    = Font size of the label (default: 12)
% 'text_pos'     = Position of the label; [1,1] corresponds to the tip; [0.5,1] corresponds to the middle of the phasor; [0.5,1.5] corresponds to the middle of the phasor plus 0.5*abs(phasor) (90 degrees to the phasor) (default: [1, 1])
% 'text_color'   = Color of the label (default: 'k')
% 'line_style'   = Line style (default: '-')
% 'line_width'   = Line width (default: 1)
% 'color'        = Color of the phasor (default: 'b')
% 
% ---------Example------------
% 
% 
% j =1i;
% fig = figure;
% axis equal
% draw_vec(0,2+3*j,'text_string','$\underline{U}_1$','text_pos',[0.5,1.1])
% draw_vec(2+3*j,-1)
% draw_vec(2+3*j,-1)
% draw_vec(2+3*j,-1)
% draw_vec(1+3*j,-1-3*j,'no_arrow',1,'line_style','--')
% draw_vec(0,-1-3*j,'double_arrow',1,'line_style','-','color','r')

% Default values for optional parameters
params = struct('text_string', '', ...
                'arrowscale', 0.15, ...
                'double_arrow', false, ...
                'no_arrow', false, ...
                'text_size', 12, ...
                'text_pos', [1, 1], ...
                'text_color', 'k', ...
                'line_style', '-', ...
                'line_width', 1, ...
                'color', 'b');

% Verarbeite Name-Wert-Paare
if mod(length(varargin), 2) ~= 0
    error('Options must be specified as name-value pairs.');
end
for i = 1:2:length(varargin)
    name = varargin{i};
    value = varargin{i+1};
    if isfield(params, name)
        params.(name) = value;
    else
        error('Invalid parameter name: %s', name);
    end
end


arrowscale = params.arrowscale; 
text_string = params.text_string; 
double_arrow = params.double_arrow; 
no_arrow = params.no_arrow; 
text_size = params.text_size; 
text_pos = params.text_pos; 
text_color = params.text_color; 
line_style = params.line_style; 
line_width = params.line_width; 
color = params.color; 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
j = 1i;
% Winkel der Spitze (Gleichschenkliges Dreieck)
alpha = 60*pi/180;
real_scale = real(arrowscale*0.5*exp(j*angle(vector)));
imag_scale = imag(arrowscale*0.5*exp(j*angle(vector)));
% Zeigerschaft
if double_arrow
    plot([real(start_vec)+real_scale,real(start_vec)+real(vector)-real_scale],[imag(start_vec)+imag_scale,imag(start_vec)+imag(vector)-imag_scale],'Color',color,'LineStyle',line_style,'LineWidth',line_width,'HandleVisibility','off')
else
    if no_arrow
    plot([real(start_vec),real(start_vec)+real(vector)],[imag(start_vec),imag(start_vec)+imag(vector)],'Color',color,'LineStyle',line_style,'LineWidth',line_width,'HandleVisibility','off')
    else
    plot([real(start_vec),real(start_vec)+real(vector)-real_scale],[imag(start_vec),imag(start_vec)+imag(vector)-imag_scale],'Color',color,'LineStyle',line_style,'LineWidth',line_width,'HandleVisibility','off')
    end
end

hold on

% Zeigerspitze
if not(no_arrow)
arrow_1 = vector;
arrow_2 = arrow_1-(arrow_1-(abs(arrow_1)-arrowscale)*exp(j*angle(arrow_1))) *exp(j*alpha/2);
arrow_3 = arrow_1-(arrow_1-(abs(arrow_1)-arrowscale)*exp(j*angle(arrow_1))) *exp(-j*alpha/2);
fill([real(arrow_1)+real(start_vec),real(arrow_2)+real(start_vec),real(arrow_3)+real(start_vec)],[imag(arrow_1)+imag(start_vec),imag(arrow_2)+imag(start_vec),imag(arrow_3)+imag(start_vec)],color,'LineStyle','none','HandleVisibility','off')
end

if double_arrow
arrow_1 = start_vec;
arrow_2 = arrow_1+(arrowscale)*exp(j*angle(vector)) *exp(j*alpha/2);
arrow_3 = arrow_1+(arrowscale)*exp(j*angle(vector)) *exp(-j*alpha/2);
fill([real(arrow_1),real(arrow_2),real(arrow_3)],[imag(arrow_1),imag(arrow_2),imag(arrow_3)],color,'LineStyle','none','HandleVisibility','off')
end

% Text
text_pos = start_vec + vector*text_pos(1)+j*vector*(text_pos(2)-1);
text(real(text_pos),imag(text_pos),text_string,'Interpreter','latex','FontSize',text_size,'Color',text_color);
axis equal
end