uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

circle128.c (1389B)


      1 #include <stdio.h>
      2 #include <math.h>
      3 
      4 /*
      5 Copyright (c) 2020-2023 Devine Lu Linvega
      6 
      7 Permission to use, copy, modify, and distribute this software for any
      8 purpose with or without fee is hereby granted, provided that the above
      9 copyright notice and this permission notice appear in all copies.
     10 
     11 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12 WITH REGARD TO THIS SOFTWARE.
     13 */
     14 
     15 #define PI 3.14159265358979323846
     16 
     17 typedef unsigned char Uint8;
     18 
     19 int
     20 clamp(int val, int min, int max)
     21 {
     22 	return (val >= min) ? (val <= max) ? val : max : min;
     23 }
     24 
     25 int
     26 sint(char *s)
     27 {
     28 	int i = 0, num = 0;
     29 	while(s[i] && s[i] >= '0' && s[i] <= '9')
     30 		num = num * 10 + (s[i++] - '0');
     31 	return num;
     32 }
     33 
     34 int
     35 main(int argc, char *argv[])
     36 {
     37 	int seg, offset, i;
     38 	double segf, cx = 128, cy = 128, r;
     39 	if(argc < 2) {
     40 		printf("usage: circle128 length [radius]\n", argc);
     41 		return 1;
     42 	}
     43 	seg = sint(argv[1]);
     44 	segf = (double)seg;
     45 	offset = seg / 4;
     46 	r = argc < 3 ? 128 : (double)sint(argv[2]);
     47 	printf("%d points on a circle%d:\n\n", seg, (int)r);
     48 	for(i = 0; i < seg; ++i) {
     49 		double pos = (i - offset) % seg;
     50 		double deg = (pos / segf) * 360.0;
     51 		double rad = deg * (PI / 180);
     52 		double x = cx + r * cos(rad);
     53 		double y = cy + r * sin(rad);
     54 		if(i > 0 && i % 8 == 0)
     55 			printf("\n");
     56 		printf("%02x%02x ", (Uint8)clamp(x, 0x00, 0xff), (Uint8)clamp(y, 0x00, 0xff));
     57 	}
     58 	printf("\n\n");
     59 	return 0;
     60 }