Specifying line length in TikZ

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












(I apologise if this question has been asked before, it's hard to believe it hasn't, but I can't find any material on it.) I want to draw a line passing through two points where I can specify the width. The line should go from the first given point in the direction of the second point, then continue until the given length is up. I want to be able to type something like draw[length=5] (0,0)--(1,2); to accomplish this.



Here's what I have so far:



documentclassarticle

usepackagetikz

begindocument
begintikzpicture
draw (0,0)--(1,2);
endtikzpicture
enddocument


Again, the problem with this is that it draws a line terminating at the two given points, but I only want it to terminate at one point (the first) but continue until the length specified has been used up at the other end. I know that this can be accomplished by always manually calculating the coordinates using some math, but that is ridiculously tedious and there must be a better way to do it! Thanks in advance.










share|improve this question





















  • see if polar coordinate can be of help. using it you need to determine angle of line (with atan function, for example): draw (0,0) -- (<angle>:5cm);
    – Zarko
    2 hours ago










  • Is that the best way? It can be a bit tedious especially when one of those points isn't at the origin...
    – Teyyf
    2 hours ago










  • Why? You could do draw (10,20) -- (angle:5cm). For the calculation, you can assume the point you start the origin if you want.
    – TeXnician
    2 hours ago











  • How will I find the angle given any two arbitrary points? Do I need to do something like type it into a calculator?
    – Teyyf
    2 hours ago










  • @Teyyf, well, you need some basic knowledge from geometry (how calculate angle from given catheterizes of triangle) :-)
    – Zarko
    1 hour ago














up vote
1
down vote

favorite












(I apologise if this question has been asked before, it's hard to believe it hasn't, but I can't find any material on it.) I want to draw a line passing through two points where I can specify the width. The line should go from the first given point in the direction of the second point, then continue until the given length is up. I want to be able to type something like draw[length=5] (0,0)--(1,2); to accomplish this.



Here's what I have so far:



documentclassarticle

usepackagetikz

begindocument
begintikzpicture
draw (0,0)--(1,2);
endtikzpicture
enddocument


Again, the problem with this is that it draws a line terminating at the two given points, but I only want it to terminate at one point (the first) but continue until the length specified has been used up at the other end. I know that this can be accomplished by always manually calculating the coordinates using some math, but that is ridiculously tedious and there must be a better way to do it! Thanks in advance.










share|improve this question





















  • see if polar coordinate can be of help. using it you need to determine angle of line (with atan function, for example): draw (0,0) -- (<angle>:5cm);
    – Zarko
    2 hours ago










  • Is that the best way? It can be a bit tedious especially when one of those points isn't at the origin...
    – Teyyf
    2 hours ago










  • Why? You could do draw (10,20) -- (angle:5cm). For the calculation, you can assume the point you start the origin if you want.
    – TeXnician
    2 hours ago











  • How will I find the angle given any two arbitrary points? Do I need to do something like type it into a calculator?
    – Teyyf
    2 hours ago










  • @Teyyf, well, you need some basic knowledge from geometry (how calculate angle from given catheterizes of triangle) :-)
    – Zarko
    1 hour ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











(I apologise if this question has been asked before, it's hard to believe it hasn't, but I can't find any material on it.) I want to draw a line passing through two points where I can specify the width. The line should go from the first given point in the direction of the second point, then continue until the given length is up. I want to be able to type something like draw[length=5] (0,0)--(1,2); to accomplish this.



Here's what I have so far:



documentclassarticle

usepackagetikz

begindocument
begintikzpicture
draw (0,0)--(1,2);
endtikzpicture
enddocument


Again, the problem with this is that it draws a line terminating at the two given points, but I only want it to terminate at one point (the first) but continue until the length specified has been used up at the other end. I know that this can be accomplished by always manually calculating the coordinates using some math, but that is ridiculously tedious and there must be a better way to do it! Thanks in advance.










share|improve this question













(I apologise if this question has been asked before, it's hard to believe it hasn't, but I can't find any material on it.) I want to draw a line passing through two points where I can specify the width. The line should go from the first given point in the direction of the second point, then continue until the given length is up. I want to be able to type something like draw[length=5] (0,0)--(1,2); to accomplish this.



Here's what I have so far:



documentclassarticle

usepackagetikz

begindocument
begintikzpicture
draw (0,0)--(1,2);
endtikzpicture
enddocument


Again, the problem with this is that it draws a line terminating at the two given points, but I only want it to terminate at one point (the first) but continue until the length specified has been used up at the other end. I know that this can be accomplished by always manually calculating the coordinates using some math, but that is ridiculously tedious and there must be a better way to do it! Thanks in advance.







tikz-pgf draw






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









Teyyf

946




946











  • see if polar coordinate can be of help. using it you need to determine angle of line (with atan function, for example): draw (0,0) -- (<angle>:5cm);
    – Zarko
    2 hours ago










  • Is that the best way? It can be a bit tedious especially when one of those points isn't at the origin...
    – Teyyf
    2 hours ago










  • Why? You could do draw (10,20) -- (angle:5cm). For the calculation, you can assume the point you start the origin if you want.
    – TeXnician
    2 hours ago











  • How will I find the angle given any two arbitrary points? Do I need to do something like type it into a calculator?
    – Teyyf
    2 hours ago










  • @Teyyf, well, you need some basic knowledge from geometry (how calculate angle from given catheterizes of triangle) :-)
    – Zarko
    1 hour ago
















  • see if polar coordinate can be of help. using it you need to determine angle of line (with atan function, for example): draw (0,0) -- (<angle>:5cm);
    – Zarko
    2 hours ago










  • Is that the best way? It can be a bit tedious especially when one of those points isn't at the origin...
    – Teyyf
    2 hours ago










  • Why? You could do draw (10,20) -- (angle:5cm). For the calculation, you can assume the point you start the origin if you want.
    – TeXnician
    2 hours ago











  • How will I find the angle given any two arbitrary points? Do I need to do something like type it into a calculator?
    – Teyyf
    2 hours ago










  • @Teyyf, well, you need some basic knowledge from geometry (how calculate angle from given catheterizes of triangle) :-)
    – Zarko
    1 hour ago















see if polar coordinate can be of help. using it you need to determine angle of line (with atan function, for example): draw (0,0) -- (<angle>:5cm);
– Zarko
2 hours ago




see if polar coordinate can be of help. using it you need to determine angle of line (with atan function, for example): draw (0,0) -- (<angle>:5cm);
– Zarko
2 hours ago












Is that the best way? It can be a bit tedious especially when one of those points isn't at the origin...
– Teyyf
2 hours ago




Is that the best way? It can be a bit tedious especially when one of those points isn't at the origin...
– Teyyf
2 hours ago












Why? You could do draw (10,20) -- (angle:5cm). For the calculation, you can assume the point you start the origin if you want.
– TeXnician
2 hours ago





Why? You could do draw (10,20) -- (angle:5cm). For the calculation, you can assume the point you start the origin if you want.
– TeXnician
2 hours ago













How will I find the angle given any two arbitrary points? Do I need to do something like type it into a calculator?
– Teyyf
2 hours ago




How will I find the angle given any two arbitrary points? Do I need to do something like type it into a calculator?
– Teyyf
2 hours ago












@Teyyf, well, you need some basic knowledge from geometry (how calculate angle from given catheterizes of triangle) :-)
– Zarko
1 hour ago




@Teyyf, well, you need some basic knowledge from geometry (how calculate angle from given catheterizes of triangle) :-)
– Zarko
1 hour ago










1 Answer
1






active

oldest

votes

















up vote
2
down vote













If you know the angle, then you can use relative polar coodinates, e.g. draw (1,1) -- +(30:2cm);. The + before the polar coordinate indicates that the previous coordinate ((1,1)) should be used as the origin. If you use ++(30:2cm), the current point is also updated, so if you add another relative coordinate to the path, the origin of that will be the end point of the first segment.



If you have two points, you can use the features of the calc library. ($(a)!3cm!(b)$) is the coordinate that is 3cm away from a, at the line passing through a and b. Hence, draw (a) -- ($(a)!3cm!(b)$); draws a line from a to that point.



enter image description here



documentclass[border=5mm]standalone
usepackagetikz
usetikzlibrarycalc
begindocument
begintikzpicture
draw (0,0) grid (4,3);

node[inner sep=1pt,fill, label=above left:a] (a) at (1,1) ;
node[inner sep=1pt,fill, label=above left:b] (b) at (3,2) ;

draw [red] (a) -- +(30:2cm);

draw [blue] (a) -- ($(a)!3cm!(b)$);

endtikzpicture
enddocument





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "85"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f452060%2fspecifying-line-length-in-tikz%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    If you know the angle, then you can use relative polar coodinates, e.g. draw (1,1) -- +(30:2cm);. The + before the polar coordinate indicates that the previous coordinate ((1,1)) should be used as the origin. If you use ++(30:2cm), the current point is also updated, so if you add another relative coordinate to the path, the origin of that will be the end point of the first segment.



    If you have two points, you can use the features of the calc library. ($(a)!3cm!(b)$) is the coordinate that is 3cm away from a, at the line passing through a and b. Hence, draw (a) -- ($(a)!3cm!(b)$); draws a line from a to that point.



    enter image description here



    documentclass[border=5mm]standalone
    usepackagetikz
    usetikzlibrarycalc
    begindocument
    begintikzpicture
    draw (0,0) grid (4,3);

    node[inner sep=1pt,fill, label=above left:a] (a) at (1,1) ;
    node[inner sep=1pt,fill, label=above left:b] (b) at (3,2) ;

    draw [red] (a) -- +(30:2cm);

    draw [blue] (a) -- ($(a)!3cm!(b)$);

    endtikzpicture
    enddocument





    share|improve this answer
























      up vote
      2
      down vote













      If you know the angle, then you can use relative polar coodinates, e.g. draw (1,1) -- +(30:2cm);. The + before the polar coordinate indicates that the previous coordinate ((1,1)) should be used as the origin. If you use ++(30:2cm), the current point is also updated, so if you add another relative coordinate to the path, the origin of that will be the end point of the first segment.



      If you have two points, you can use the features of the calc library. ($(a)!3cm!(b)$) is the coordinate that is 3cm away from a, at the line passing through a and b. Hence, draw (a) -- ($(a)!3cm!(b)$); draws a line from a to that point.



      enter image description here



      documentclass[border=5mm]standalone
      usepackagetikz
      usetikzlibrarycalc
      begindocument
      begintikzpicture
      draw (0,0) grid (4,3);

      node[inner sep=1pt,fill, label=above left:a] (a) at (1,1) ;
      node[inner sep=1pt,fill, label=above left:b] (b) at (3,2) ;

      draw [red] (a) -- +(30:2cm);

      draw [blue] (a) -- ($(a)!3cm!(b)$);

      endtikzpicture
      enddocument





      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        If you know the angle, then you can use relative polar coodinates, e.g. draw (1,1) -- +(30:2cm);. The + before the polar coordinate indicates that the previous coordinate ((1,1)) should be used as the origin. If you use ++(30:2cm), the current point is also updated, so if you add another relative coordinate to the path, the origin of that will be the end point of the first segment.



        If you have two points, you can use the features of the calc library. ($(a)!3cm!(b)$) is the coordinate that is 3cm away from a, at the line passing through a and b. Hence, draw (a) -- ($(a)!3cm!(b)$); draws a line from a to that point.



        enter image description here



        documentclass[border=5mm]standalone
        usepackagetikz
        usetikzlibrarycalc
        begindocument
        begintikzpicture
        draw (0,0) grid (4,3);

        node[inner sep=1pt,fill, label=above left:a] (a) at (1,1) ;
        node[inner sep=1pt,fill, label=above left:b] (b) at (3,2) ;

        draw [red] (a) -- +(30:2cm);

        draw [blue] (a) -- ($(a)!3cm!(b)$);

        endtikzpicture
        enddocument





        share|improve this answer












        If you know the angle, then you can use relative polar coodinates, e.g. draw (1,1) -- +(30:2cm);. The + before the polar coordinate indicates that the previous coordinate ((1,1)) should be used as the origin. If you use ++(30:2cm), the current point is also updated, so if you add another relative coordinate to the path, the origin of that will be the end point of the first segment.



        If you have two points, you can use the features of the calc library. ($(a)!3cm!(b)$) is the coordinate that is 3cm away from a, at the line passing through a and b. Hence, draw (a) -- ($(a)!3cm!(b)$); draws a line from a to that point.



        enter image description here



        documentclass[border=5mm]standalone
        usepackagetikz
        usetikzlibrarycalc
        begindocument
        begintikzpicture
        draw (0,0) grid (4,3);

        node[inner sep=1pt,fill, label=above left:a] (a) at (1,1) ;
        node[inner sep=1pt,fill, label=above left:b] (b) at (3,2) ;

        draw [red] (a) -- +(30:2cm);

        draw [blue] (a) -- ($(a)!3cm!(b)$);

        endtikzpicture
        enddocument






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 43 mins ago









        Torbjørn T.

        151k13243424




        151k13243424



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f452060%2fspecifying-line-length-in-tikz%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery