TransWikia.com

Get length of segments. The result is not the same in different scale

TeX - LaTeX Asked by ducanh2021 on August 2, 2021

I have a marco mylengthcm to get length of segments. But the result is not the same in different scale. What do I have to do to get the same result? Thank you for helping me.
This is my code

documentclass{article}
usepackage{tikz,tkz-euclide}
usepackage{xfp}
makeatletter
defmylengthcm(#1,#2)#3{%
    pgfpointdiff{pgfpointanchor{#1}{center}}%
                 {pgfpointanchor{#2}{center}}%
    edefxfpdodaipt{fpeval{sqrt((pgf@x)^2+(pgf@y)^2)}}
    %pt to cm
    pgfmathparse{xfpdodaipt pt/1cm}
    edefxfpdodaicm{fpeval{round(pgfmathresult,7)}}
    globalexpandafteredefcsname #3endcsname{xfpdodaicm}
    }%%
makeatother
begin{document}
begin{tikzpicture}[font=small,scale=.5]
draw[help lines] (-6,-6) grid (6,6);
coordinate (A) at (0,0);
coordinate (B) at (3,4);
mylengthcm(A,B){rcm} 
draw[teal](A)--(B)node[midway, sloped, below]{$rcm$ cm};
draw[teal] (A)circle(rcm);
end{tikzpicture}
%
begin{tikzpicture}[font=small,scale=0.8]
draw[help lines] (-6,-6) grid (6,6);
coordinate (A) at (0,0);
coordinate (B) at (3,4);
mylengthcm(A,B){rcm}
draw[teal](A)--(B)node[midway, sloped, below]{$rcm$ cm};
draw[teal] (A)circle(rcm);
end{tikzpicture}
newline
%usepackage{tkz-euclide}
begin{tikzpicture}[scale=2]
    tkzDefPoint(0,0){A}
    tkzDefPoint(3,4){B}
    tkzCalcLength(A,B)tkzGetLength{dAB}
    tkzCalcLength[cm](A,B)tkzGetLength{rAB}
%%%%%
    draw(A)--(B)node[midway, sloped, above]
        {$dAB mathrm{pt}$ = $rAB mathrm{cm}$};
    foreach p/pos in {A/-90,B/-90}
        fill(p)circle(1.pt)node[shift={(pos:8pt)}]{$p$};
end{tikzpicture}
end{document}

One Answer

Here is one way that work with any scaling. As you see, the error of calculations is about 10^{-4}, just like TeX's calculation.

enter image description here

documentclass[border=5mm]{standalone}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
% from https://www.facebook.com/groups/TikZandAsymptote 
% tikzlength(A,B)(c) >> c is the length in cm of AB
deftikzlength(#1,#2)(#3){%
path let p1 = ($(#1)-(#2)$),
          n1 = {scalar(veclen(x1,y1)/1cm)}
in pgfextra{xdef#3{n1}};
}%
begin{tikzpicture}
path
(0,0) coordinate (A)    
(4,0) coordinate (B)
(0,3) coordinate (C)
;
draw (C)--(A)--(B);
tikzlength(B,C)(a)
draw[blue] (B)--(C) node[sloped,above,midway]{$a$ cm};
end{tikzpicture}
begin{tikzpicture}[scale=.5]
    path
    (0,0) coordinate (A)    
    (4,0) coordinate (B)
    (0,3) coordinate (C)
    ;
    draw (C)--(A)--(B);
    tikzlength(B,C)(a)
    draw[red] (B)--(C) node[sloped,above,midway]{$a$ cm};
end{tikzpicture}

end{document}  

PS: In Asymptote, there is a built-in function length (same as abs) that gives the length for both 2D and 3D points. Accuracy of Asymptote is about 10^{-18} (?).

enter image description here

unitsize(1cm);
pair A=(0,0), B=(4,0), C=(0,3);
real a=length(B-C);

picture pic1,pic2;

draw(pic1,C--A--B);
draw(pic1,Label(string(a)+" cm"),B--C,magenta);
add(pic1);

draw(pic2,C--A--B);
draw(pic2,Label(string(a)+" cm"),B--C,purple);
add(shift(5,0)*scale(.5)*pic2);

shipout(bbox(5mm,invisible)); 

Correct answer by Black Mild on August 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP