Labelling a tikz picture in Latex - label

\begin{tikzpicture}
\filldraw[color=black!60, fill=white!5, ultra thick](-1,0) circle (0.8);
\filldraw[color=black!60, fill=white!5, ultra thick](5,0) circle (0.8);
\draw[black, ultra thick] (-1,0) -- (5,0);
\end{tikzpicture}
This code creates a picture with two circles and a line joining their centres. I need to label the left circle as Left Circle and the right one as Right Circle. Both labels have to be at the top of the circles, NOT below them.
Can I please have some help with this?

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (-1,0);
\coordinate (B) at (5,0);
\filldraw[color=black!60, fill=white!5, ultra thick] (A) circle (0.8);
\filldraw[color=black!60, fill=white!5, ultra thick] (B) circle (0.8);
\draw[black, ultra thick] (A) -- (B);
\node[yshift=1.2cm] at (A) {left circle};
\node[yshift=1.2cm] at (B) {right circle};
\end{tikzpicture}
\end{document}

Related

How can i change the graphviz.subgrah's shape to ellipse or circle?

When i use graphviz's subgraph in python, I found it's hard to change subgraph's shape from rectangle to
ellipse or circle. How can i do this?
the code is below
from graphviz import Graph
g = Graph('G', filename='fdpclust.gv', engine='fdp')
with g.subgraph(name='clusterA',graph_attr ={'shape':'ellipse'}) as a:
a.node('a',fontname="SimSun",fontsize='29')
a.node('b')
with a.subgraph(name='clusterC',graph_attr ={'shape':'ellipse'}) as c:
c.attr(shape='ellipse')
c.node('C')
c.node('D')
with g.subgraph(name='clusterB',graph_attr ={'shape':'circle'}) as b:
b.node('d')
b.node('f')
g.view()
the image is:
You can't set the shape of clusters. The closest you can do is setting the style to rounded to get round corners

How do I create the following illustration in LaTeX?

I have tried to create the following illustration in LaTex with poor results. The problem is that I am not able to determine the starting points nor ending points of the arrows how I want to. Any suggestions/hints how to do succeed in this?
Here is possible implementation using tikz, with the calc and positioning libraries
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[box/.style={
draw,thick,
},
label/.style={draw=none,midway,sloped,above,font=\small},
myarrow/.style={->,thick} ,
]
%poition upper and lower nodes
\node[box] (gov) {Government};
\node[box,below=4cm of gov,minimum width=3cm,dotted] (trans) {Transaction} ;
% position first line extremity at, say, 1/3 gov node
\coordinate (extr1) at ($(trans.north west)!0.33!(trans.north east)$) ;
% then extremity 2 slightly at the right of extr1
\coordinate (extr2) at ([xshift=2mm]extr1) ;
% position bank vertically halfway gov/trans and centered wrt extr1 and extr2
% due to some limitations in tikz parser, one must first define intermediate coords
\coordinate (extr12) at ($(extr1)!0.5!(extr2)$) ;
\coordinate (mid) at ($(gov)!0.5!(trans)$);
\node[box] (bank) at (extr12 |- mid) {Bank};
% draw first arrows
\draw[myarrow] (extr1) -- (extr1 |- bank.south) node[label] {information};
\draw[myarrow] (extr2 |- bank.south) -- (extr2) node[label] {monitoring};
\draw[myarrow] (extr1 |- bank.north) -- (extr1 |- gov.south) node[label] {reporting} ;
\draw[myarrow] (extr2 |- gov.south) -- (extr2 |- bank.north) node[label] {fines} ;
% position last arrow midway from east border of bank and gov
\coordinate (extr3) at ($(bank.east)!0.5!(gov.east)$) ;
\draw[myarrow] (extr3 |- gov.south) -- (extr3 |- trans.north) node[label] {investigation} ;
\end{tikzpicture}
\end{document}
You have to modify a couple parameters for the positioning that you want and add style to nodes/lines as required.
Using north and south (and phantom) makes it ok vertically. Instead, I wanted to use calc TikZ library to also horizontally offset endpoints of arrows from nodes (a), (b), (c) in a more elegant way but I didn't succeed:
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{arrows,decorations.markings}
\begin{document}
\begin{tikzpicture}[>=triangle 45, thick, sloped]
\node [rectangle, very thick, draw, right] (a) at (0,6) {\large\textbf{Government}};
\node [rectangle, very thick, draw, right] (b) at (0,3) {\large\textbf{Bank}};
\node [rectangle, densely dotted, draw, right] (c) at (0,0) {\large Transaction};
\node (d) at (.2,6) {\phantom{G}};
\node (e) at (.2,3) {\phantom{B}};
\node (f) at (.2,0) {\phantom{T}};
\node (g) at (1.1,6) {\phantom{G}};
\node (h) at (1.1,3) {\phantom{B}};
\node (i) at (1.1,0) {\phantom{T}};
\node (j) at (2,6) {\phantom{G}};
\node (k) at (2,0) {\phantom{T}};
\draw [->] (e.north) -- (d.south) node[pos=0.5, above] {reporting};
\draw [->] (g.south) -- (h.north) node[pos=0.5, above] {fines};
\draw [->] (f.north) -- (e.south) node[pos=0.5, above] {information};
\draw [->] (h.south) -- (i.north) node[pos=0.5, above] {monitoring};
\draw [->] (j.south) -- (k.north) node[pos=0.5, above] {investigation};
\end{tikzpicture}
\end{document}
This code results in:
Then you can tune this depending on your taste!

Find all cells on a grid which is in circle?

I have a grid which is 100 by 100, and I have a circle. I want the circle to put a value in every cell that the circle covers. Is there a good algorithm for this ? I will be using as3 but I don't think that would be a issue.
edit.
I'm trying to find a algorithm which will return all cells within the circle and all cells which are semi in the circle / mostly in the circle (Greater than 50%). As I haven't found algorithm which does this I cannot show any code.
I know this is an old question but might be useful for anyone (like me) looking for an answer here.
The (pseudocode) approach I took was to work out a bounding box around the circle of centre a,b radius r using two opposite corners:
topLeft = (x:a - r, y:b + r)
bottomRight: (x:a + r, y:b - r)
Then iterate over all squares within the bounding box and test if they are in the circle
for(x between topLeft.x and bottomRight.x){
for(y between topLeft.y and bottomRight.y){
if((x-a)^2 + (y-b)^2 < r^2){
// point is in circle
}
}
}
Start at the centre of the circle. Mark that grid square. Now construct a spiral starting from that square, checking each grid square along the spiral and marking as appropriate. There will be some formula for how long each new turn of the spiral will be. When a full turn of the spiral is outside the circle you do not need to check any more grid squares.

Matlab: Flipping image coordinates to cartesian coordinates

I am new to Matlab. I have an image (the size is mxnx3) with a few human-selected points on the image. For example:
p1 = [267,79];
p2 = [96,372];
These points are image coordinates with (1,1) at the top left. I'm trying to convert this to Cartesian coordinates with (0,0) on the bottom left. How can I do this? Thanks in advance!
If I understand correctly: just use
axis xy
From axis doc:
AXIS XY puts MATLAB into its default "Cartesian" axes mode. The
coordinate system origin is at the lower left corner. The x
axis is horizontal and is numbered from left to right. The y
axis is vertical and is numbered from bottom to top.
If you need to directly translate your co-ordinates in code, you could make a simple anonymous function:
img2cart = #(p) [p(1), img.size(2) - p(2)];
q1 = img2cart(p1);
q2 = img2cart(p2);

Ellipse thickness algorithm

Does anybody knows an algorithm for drawing an ellipse with thickness?
I googled, but I found only algorithms that draws an 1 pixel width ellipse, like this:
http://homepage.smc.edu/kennedy_john/belipse.pdf
Thanks.
By an ellipse with thickness do you mean the difference between two ellipses, one where the two axes have been lengthened by 1/2-thickness and the other where they have been shortened by 1/2-thickness?
If so, then you can adapt the linked algorithm into a scanline fill algorithm. One thing you want to do is work only along the shorter axis. (working along the longer axis works too, but involves redundant computation).
Let's say it's wider than it is tall. (If it's the other way around you just flip axes when drawing.) In that case you'll be drawing one or two horizontal line segments for each y position.
For each value of y from the top of the outer ellipse to the center of the ellipses:
If y is above inner ellipse:
Draw one horizontal line segment from the upper left quadrant point on the outer ellipse to the upper right quadrant point on the outer ellipse.
Else (y is not above inner ellipse):
Draw two horizontal line segments:
One from the upper left quadrant point of the outer ellipse to the upper left quadrant point of the inner ellipse.
Another from the upper right quadrant point of the inner ellipse to the upper right quadrant point of the outer ellipse.
Either way, mirror all drawing over the x-axis of the ellipses to
render the bottom two quadrants.
how accurate do you need to be?
do you want the real ellipse point in the approximate center of the 'x' pixel width border? have the real elipse point be the inside edge? outside edge?
I ask b/c the gentleman's algorithm you found strives to stick with integer math where possible, so I'll append to his algorithm with integer work as well.
inside edge: alter the Plot4EllipsePoints sub routine to paint x pixels instead of one, where the new x pixels are further away from the ellipse center. 2 pixel eg:
procedure Plot4EllipsePoints(X,Y : longint);
begin
PutPixel(CX+X, CY+Y); {point in quadrant 1}
PutPixel(CX+X+1, CY+Y+1); {point in quadrant 1}
PutPixel(CX-X, CY+Y); {point in quadrant 2}
PutPixel(CX-X-1, CY+Y+1); {point in quadrant 2}
PutPixel(CX-X, CY-Y); {point in quadrant 3}
PutPixel(CX-X-1, CY-Y-1); {point in quadrant 3}
PutPixel(CX+X, CY-Y) {point in quadrant 4}
PutPixel(CX+X+1, CY-Y-1) {point in quadrant 4}
end;
taken from :http://homepage.smc.edu/kennedy_john/belipse.pdf
outside edge: same as inside edge, but closer to the ellipse center.
centered: perform the inside edge +
outside edge both. This will only have odd thicknesses, 1 pix, 3 pix, 5 pix.
Let E1 be an ellipse of radius r + thickness / 2 and E2 an ellipse of radius r - thickness / 2.
Adapt the Scanline Fill Algorithm to fill E1 without filling E2.

Resources