It would be convenient sometimes to be able to draw an arc in tikz
by specifying
i.e., the "natural" way an arc is defined, instead of the "first point of the arc".
Is there a way to do it?
You can use the parametrization
x(t)=a+r*cos(t)
y(t)=b+r*sin(t)
where r
is the radius of the circle and (a,b)
are the coordinates of its center. In Tikz this can be implemented as follows:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [red,thick,domain=0:90] plot ({cos(\x)}, {sin(\x)});
\draw [blue,thick,domain=180:270] plot ({cos(\x)}, {sin(\x)});
\end{tikzpicture}
\end{document}
which produces
Depending on your application, you might like to do this using the pgfplots
package.
Also, in case this is used frequently, consider defining a custom command \centerarc
as suggested in a comment by Tom Bombadil (this requires \usetikzlibrary{calc}
):
\def\centerarc[#1](#2)(#3:#4:#5)% Syntax: [draw options] (center) (initial angle:final angle:radius)
{ \draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5); }
Then use it by invoking
\centerarc[red,thick](0,0)(5:85:1)
\def\centerarc[#1](#2)(#3:#4:#5)% [draw options] (center) (initial angle:final angle:radius) { \draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5); }
It. can be used e.g. likr this: \centerarc[red,thick](0,0)(5:85:1)
- Tom Bombadil
\usetikzlibrary{calc}
- PatrickT
You should use a coordinate transformation for this, to get a proper starting point of the arc. Say, ([shift=(t:r)] x, y)
is the proper starting point, where (x,y)
is the center and (t:r)
is the polar coordinate of starting point.
Full example:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (4,3);
\draw (2,1) -- ++(30:2cm)
(2,1) -- ++(60:2cm);
% Draw the arc which center is (2,1)
\draw[thick,red] ([shift=(30:1cm)]2,1) arc (30:60:1cm);
\end{tikzpicture}
\end{document}
Notation: each arc is defined by <center>
, <radius>
, <init angle>
and <final angle>
.
If you want to be able to link several arcs in a single path, you can use shift
with following syntax:
initial point:([shift={(<init angle>:<radius>)}]<center>)
to draw your arc: arc (<init angle>:<final angle>:<radius>)
Example (orange path uses proposed syntax and cyan path uses a style):
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[fill=orange]
([shift={(-40:1cm)}]-1.1,0) arc (-40:40:1cm)
--
([shift={(-40+180:1cm)}]1.1,0) arc (-40+180:40+180:1cm)
-- cycle;
\end{tikzpicture}
\begin{tikzpicture}
\tikzset{translate/.style={shift={(#1)}}}
\draw[fill=cyan]
([translate=-40:1cm]-1.1,0) arc (-40:40:1cm)
--
([translate=-40+180:1cm]1.1,0) arc (-40+180:40+180:1cm)
-- cycle;
\end{tikzpicture}
\end{document}
Edit: a simpler notation!
Using calc
TikZ library, you can use a simpler notation:
($(<center>) + (<init angle>:<radius>)$)
.Example:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
% center c1
\coordinate (c1) at (0,0);
\draw[fill=green]
% radius=3mm, initial=0, final=90
%([shift={(0:3mm)}]c1) arc (0:90:3mm)
($(c1) + (0:3mm)$) arc (0:90:3mm)
--
% radius=4mm, reversed
($(c1) + (90:4mm)$) arc (90:0:4mm)
-- cycle;
\draw[fill=yellow]
% radius=4mm, initial=22.5, final=180
($(c1) + (22.5:4mm)$) arc (22.5:180:4mm)
--
% radius=5mm, reversed
($(c1) + (180:5mm)$) arc (180:22.5:5mm)
-- cycle;
% center c2
\coordinate (c2) at (0,12mm);
\draw[fill=red]
% radius=5mm, initial=45, final=270
($(c2) + (45:5mm)$) arc (45:270:5mm)
--
% radius=6mm, reversed
($(c2) + (270:6mm)$) arc (270:45:6mm)
-- cycle;
\draw[fill=gray]
% radius=6mm, initial=67.5, final=360
($(c2) + (67.5:6mm)$) arc (67.5:360:6mm)
--
% radius=7mm, reversed
($(c2) + (360:7mm)$) arc (360:67.5:7mm)
-- cycle;
\end{tikzpicture}
\end{document}
calc
notation was exactly what I was looking for. Thanks! - Jeffrey Girard
Possible is to use tkz-euclide
. I f you want something independent of tkz-euclide
you can take the code inside the file tkz-obj-arcs.tex
. In each cases, I use the center
1) \tkzDrawArc and towards
towards
is the option by defaut so it's not necessary to indicate this option. In the the example, the arc starts from A towards the axe OB.
\begin{tikzpicture}[scale=1.5]
\tkzDefPoint(0,0){O}
\tkzDefPoint(2,-1){A}
\tkzDefPoint(1,1){B}
\tkzDrawArc[color=blue](O,A)(B)
\tkzDrawArc[color=Maroon](O,B)(A)
\tkzDrawArc(O,B)(A)
\tkzDrawLines[add = 0 and .5](O,A O,B)
\tkzDrawPoints(O,A,B)
\tkzLabelPoints[below](O,A,B)
\end{tikzpicture}
2) \tkzDrawArc and rotate
Here the center is O, the arc starts from A and the measure of the angle is 180 degrees.
\begin{tikzpicture}
\tkzDefPoint(0,0){O}
\tkzDefPoint(2,-2){A}
\tkzDefPoint(60:2){B}
\tkzDrawLines[add = 0 and .5](O,A O,B)
\tkzDrawArc[rotate,color=red](O,A)(180)
\tkzDrawPoints(O,A,B)
\tkzLabelPoints[below](O,A,B)
\end{tikzpicture}
3) \tkzDrawArc and R
In this case the center is O and you need to give the radius R and two angles
\begin{tikzpicture}
\tkzDefPoints{0/0/O}
\tikzset{compass style/.append style={<->}}
\tkzDrawArc[R, color=orange,double](O,3cm)(270,360)
\tkzDrawArc[R, color=blue,double](O,2cm)(0,270)
\tkzDrawPoint(O)
\tkzLabelPoint[below](O){$O$}
\end{tikzpicture}
4) \tkzDrawArc and R with nodes
In this case, we need to know the center, the radius and the arc starts from the line BA towards the line BO
\begin{tikzpicture}
\tkzDefPoint(0,0){O}
\tkzDefPoint(2,-1){A}
\tkzDefPoint(1,1){B}
\tkzCalcLength(B,A)\tkzGetLength{radius}
\tkzDrawArc[R with nodes](B,\radius pt)(A,O)
\end{tikzpicture}
5) \tkzDrawArc and delta
Useful to add an arc like with a compass.
\begin{tikzpicture}
\tkzInit
\tkzDefPoint(0,0){A} \tkzDefPoint(5,0){B} \tkzDefPointBy[rotation= center A%
angle 60](B) \tkzGetPoint{C}
\tkzSetUpLine[color=gray]
\tkzDefPointBy[symmetry= center C](A)
\tkzGetPoint{D}
\tkzDrawSegments(A,B A,D)
\tkzDrawLine(B,D)
\tkzSetUpCompass[color=orange]
\tkzDrawArc[delta=10](A,B)(C)
\tkzDrawArc[delta=10](B,C)(A)
\tkzDrawArc[delta=10](C,D)(D)
\tkzDrawPoints(A,B,C,D)
\tkzLabelPoints(A,B,C,D)
\tkzMarkRightAngle(D,B,A)
\end{tikzpicture}
tkz-euclide
, but LaTeX complains that \tkzDrawArc
is not defined. Is there something else I need to load? - A.Ellett
\usetkzobj{arcs}
or \usetkzobj{all}
- Alain Matthes
You can create a new command, like this one
\documentclass[12pt]{standalone}
\usepackage{tikz}
\newcommand{\cercle}[4]{
\node[circle,inner sep=0,minimum size={2*#2}](a) at (#1) {};
\draw[red,thick] (a.#3) arc (#3:{#3+#4}:#2);
}
\begin{document}
\begin{tikzpicture}
\coordinate (center) at (3,2);
\cercle{center}{2cm}{25}{-90}
![\cercle{4,5}{1cm}{15}{130}][1]
\end{tikzpicture}
\end{document}
Just repeat the first coordinate
%\draw (center) +(th0:rad) arc +(th0:th1:rad)
\draw (1,2) +(-20:3) arc (-20:40:3) ;
which will draw an arc with the center at (1,2)
, a radius of 3
, covering an angle from -20
to 40
degrees. To use units (e.g. cm) one can replace the 3
with 3cm
.
\draw (1,2)
command starts the path at the coordinate of the arc center;+(-20:3)
moves to the starting point of the arc (in the polar direction theta=20deg
at a distance rad=3
from the center). The +
notation means that the reference position for the path is unchanged by this move;arc
, but keeps the original center position (1,2)
.(corrected as commented by Ignasi)
(-20:1)
should be 2 or 2 from (-20:40:2)
should be 1 because the displacement from center must coincide with radius. - Ignasi
\draw (90:\R) arc (90:180:\R)
in my case. - Yaroslav Nikitenko
A combination of Leo Liu's and cmhughes' solutions (ie, without the calc
library):
\documentclass{standalone}
\usepackage{tikz}
\def\centerarc[#1](#2)(#3:#4:#5);%
%Syntax: [draw options] (center) (initial angle:final angle:radius)
{
%\draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5);
\draw[#1]([shift=(#3:#5)]#2) arc (#3:#4:#5);
}
\begin{document}
\begin{tikzpicture}[>=stealth]
\draw[help lines] (-2,-2) grid (2,2);
\centerarc[thick,<->,blue](1,1)(70:195:1cm);
\centerarc[thick,blue](1,1)(-90:25:1cm);
\draw[thin,dashed,blue] (1,1) circle (1cm);
\centerarc[thick,<->,red](0,0)(165:285:2cm);
\centerarc[thick,red](0,0)(-45:45:2cm);
\draw[thin,dashed,red] (0,0) circle (2cm);
\end{tikzpicture}
\end{document}
Remark: As seen below, the <->
option in the \centerarc
examples seems to distort the arcs themselves. I believe this is an unwanted effect, or even a bug.
I added line width and color to your command @rpapa, I find it pretty useful and very handy! Coupled with some very simple polar coordinates, it can handle a lot.
\documentclass{standalone}
\usepackage{tikz}
\newcommand{\cercle}[6]{
\node[circle,inner sep=0,minimum size={2*#2}](a) at (#1) {};
\draw[#6,line width=#5] (a.#3) arc (#3:{#3+#4}:#2);
}
% USAGE: \cercle {center} {radius in cm} {begin degrees} {value of the arc} {line width} {color}
% \coordinate (OO) at (2.8,2.2);
% \cercle{OO}{0.5cm}{-20}{60}{1.0pt}{blue};
\begin{document}
\begin{tikzpicture}
\coordinate (OR) at (0.00, 0.00);
\filldraw [orange] (OR) circle(2pt);
\node[above left, orange] at (OR) {\textbf{\textit{0}}};
\draw[step = 0.1cm, gray, ultra thin] (-1.5,-2) grid (1.5,2);
\draw[black, thin] (-1.5,0)--(1.5,0) (0,-2)--(0,2);
% 30 degree arc, starting at +15 degrees
\cercle {OR} {0.5cm} {15} {30} {1.00} {red};
\draw[red,thin] (OR) -- (pi/12 r:0.5);
\draw[red,thin] (OR) -- (pi/4 r:0.5);
% 75 degree arc, starting at -15 degrees
\cercle {OR} {1.0cm} {-15} {75} {1.50} {blue};
\draw[blue,thin] (OR) -- (-pi/12 r:1.0);
\draw[blue,thin] (OR) -- (pi/3 r:1.0);
% 90 degree arc, starting at -135 degrees
\cercle {OR} {1.5cm} {-135} {90} {2.00} {teal};
\draw[teal,thin] (OR) -- (-pi/4 r:1.5);
\draw[teal,thin] (OR) -- (-3*pi/4 r:1.5);
\end{tikzpicture}
\end{document}