TransWikia.com

How to distribute edges on a node's border evenly in TikZ

TeX - LaTeX Asked by user1494080 on February 10, 2021

In the figure below, I achieve an even distribution of the three curved edges on the border of node 3 by doing quite some math manually. This is cumbersome and inflexible. Is there a short and easy solution to let TikZ do the even distribution automatically?

documentclass{article}
usepackage{tikz}
begin{document}
begin{tikzpicture}
node [circle] (1) at (0, 0) {1};
node [circle] (2) at (1,-3) {2};
node [circle] (3) at (5,-2) {3};
node [circle] (4) at (4, 1) {4};
path (1) edge (2) (2) edge (3) (3) edge (4) (4) edge (1);

path (4) edge [out=-100, in=129.835] (3); % These in values
path (1) edge [out=-20,  in=151.236] (3); % are computed
path (2) edge [out=20,   in=172.636] (3); % manually, yuk!
end{tikzpicture}
end{document}

enter image description here

One idea of mine was to create an invisible circular arc between the point on the border of 3 where the straight lines touch, then create the start/ending points of the curved arcs by placing coordinates on that circular arc with [pos=0.25], [pos=0.5], and [pos=0.75]. However, I’m not sure if that is the cleanest solution because requires the creation of a number of helper elements and (more importantly) I have no idea how to retrieve the points on the border of 3 where the straight lines from 4 and 2 touch it.

One Answer

I propose a solution based on the pgf commands pgfmathanglebetweenpoints and pgfmathanglebetweenlines defined on page 1047 of the manual). For example, the angle determined by the points B, A, and D in this order (angleBAD in the code below) together with the angle formed by the vector AB and the horizontal axis (argAB in the code) allows us to find the angles you are looking for as argAB+k*angleBAD/4 with k=1,2,3.

In the drawing, your construction is grayed and is there for comparison. I decided, for coherence, that the inner edges must bisect the angles B, C, and D. enter image description here

The code (the local commands computearg and angle are based on @percusse 's answer pgfmathanglebetweenpoints always returns 90)

documentclass[margin=.3cm]{standalone}
usepackage{tikz}
begin{document}


newcommand{computearg}[2]{pgfmathanglebetweenpoints{pgfpointanchor{#1}{center}}{pgfpointanchor{#2}{center}}}
newcommand{computeangle}[3]{pgfmathanglebetweenlines{pgfpointanchor{#2}{center}}{pgfpointanchor{#1}{center}}{pgfpointanchor{#2}{center}}{pgfpointanchor{#3}{center}}}

begin{tikzpicture}
  node[circle] (C) at (0, 0) {C};
  node[circle] (D) at (1, -3) {D};
  node[circle] (A) at (5, -2) {A};
  node[circle] (B) at (4, 1) {B};
  path (C) edge (D) (D) edge (A) (A) edge (B) (B) edge (C);

  computearg{A}{B}
  edefargAB{pgfmathresult}
  computearg{B}{C}
  edefargBC{pgfmathresult}
  computearg{C}{D}
  edefargCD{pgfmathresult}
  computearg{D}{A}
  edefargDA{pgfmathresult}
  computeangle{B}{A}{D}
  edefangleBAD{pgfmathresult}
  computeangle{C}{B}{A}
  edefangleCBA{pgfmathresult}
  computeangle{D}{C}{B}
  edefangleDCB{pgfmathresult}
  computeangle{A}{D}{C}
  edefangleADC{pgfmathresult}

  path[red, thick]
  (A) edge[out={argAB+angleBAD/4}, in={argBC+angleCBA/2}] (B)
  (A) edge[out={argAB+angleBAD/2}, in={argCD+angleDCB/2}] (C)
  (A) edge[out={argAB+3*angleBAD/4}, in={argDA+angleADC/2}] (D);
end{tikzpicture}
end{document}

Answered by Daniel N on February 10, 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