Friday, September 27, 2013

Trickzy Tikz

So I wanted to make a regular 30-gon in tikz, but was way too lazy to work out the coordinates for each vertex on my own, so here's what I came up with:

Write a file called regpoly.c containing

#include <math.h>
#include <stdio.h>

int main(int argc, char *argv[])
int i=0;
double r=2;
double n= 30;
for (i = 0; i < n; i++) {
  printf("\\draw[-] (%f,%f) -- (%f,%f);\n",r * cos(2 * M_PI * i / n), r * sin(2 * M_PI * i / n), r * cos(2 * M_PI * (i+1)/ n), r * sin(2 * M_PI * (i+1)/ n));
return 0;

compile it in the terminal by typing

cc regpoly.c -o regpoly -lm

run it by typing


Copy the resulting output into a tikzpicture in latex. Clearly one can change the number of sides the gon has by replacing 30 in line 8. Note that wordwrap has made line 10 into two lines here, it needs to be only one for it to run.

No comments:

Post a Comment