Skip to content

Instantly share code, notes, and snippets.

@vyuh
Created October 26, 2021 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vyuh/6060948c62bcf3718ba6e3996b34c23e to your computer and use it in GitHub Desktop.
Save vyuh/6060948c62bcf3718ba6e3996b34c23e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *noram="RAM denied\n";
void die (char *msg) {
fputs (msg, stderr);
exit (0);
}
int print_indices(unsigned *dummy, unsigned *indices, unsigned count) {
unsigned i;
for(i=0; i<count; i++) {
printf("%x",indices[i]);
}
putchar('\n');
return 0;
}
int perm(unsigned n, unsigned r, unsigned *s, unsigned* t, unsigned level, void *array, int (*process) (void *array, unsigned *indices, unsigned count)) {
unsigned v;
if (level==r) {
(*process)(array, t, r);
return 0;
} else if (level<r) {
for(v=0; v<n; v++) {
if(s[v]) continue;
t[level]=v;
s[v]=1;
perm(n, r, s, t, level+1, array, process);
s[v]=0;
}
} else {
puts("level>r err!\n");
exit(0);
}
}
int permute( unsigned n, unsigned r, void *array, int (*process) (void *array, unsigned *indices, unsigned count) ) {
unsigned *t;
unsigned *s;
if (!(s = (unsigned *)malloc(n*sizeof(unsigned)))) die("no RAM alloted\n");
memset(s,0,n*sizeof(unsigned));
if (!(t = (unsigned *)malloc(r*sizeof(unsigned)))) die("no RAM alloted\n");
perm(n, r, s, t, 0, array, process);
free(t);
free(s);
return 0;
}
int choices( unsigned n, unsigned r, unsigned *choice, unsigned level, void *array, int (*process) (void *array, unsigned *indices, unsigned count)) {
while( choice[level] <= level+n-r) {
if (level == r) {
(*process)(array, choice, r);
return 0;
} else {
choice[level+1] = choice[level] + 1;
choices(n, r, choice, level+1, array, process);
}
choice[level]++;
}
return 0; /* dummy meaningless shit */
}
int choose( unsigned n, unsigned r, void *array, int (*process) (void *array, unsigned *indices, unsigned count) ) {
unsigned *choice;
if (r!=0) {
if(!(choice=malloc((r)*sizeof(unsigned)))) die(noram);
*choice = 0;
choices(n, r, choice, 0, array, process);
free(choice);
}
return 0;
}
#include <stdio.h>
typedef struct {
double x;
double y;
} point;
int print_point(point p) {
return printf( "point: %lf %lf\n", p.x, p.y);
}
#include "pk.h"
#include "point.h"
#include <math.h>
#include <assert.h>
point *regular_polygon_vertices(unsigned n) {
int i;
double PI = atan2(1,1)*4;
point * out;
if (!(out = malloc(n*sizeof(point)))) die(noram);
for ( i=0; i<n; i++ ) {
(out+i)->x = cos((PI*2*i)/n);
(out+i)->y = sin((PI*2*i)/n);
}
return out;
}
int print_regular_polygon_points (unsigned n) {
unsigned i, j;
point *points;
for ( i=3; i<=n; i++ ) {
points = regular_polygon_vertices(i);
printf("Regular Polygon with %d sides\n", i);
for ( j=0; j<i; j++ ) {
print_point( *(points+j) );
}
free(points);
}
return 0;
}
int svg_line( point a, point b) {
printf(
"<line x1=\"%lf\" y1=\"%lf\" x2=\"%lf\" y2=\"%lf\" stroke=\"black\" stroke-width=\"0.001\"/>\n",
a.x, a.y, b.x, b.y
);
return 0;
}
int svg_line_point_pair(point *points, unsigned *indices, unsigned n) {
assert(n==2);
svg_line( *(points+indices[0]), *(points+indices[1]) );
return 0;
}
int print_points(point *points, unsigned *indices, unsigned n) {
unsigned i;
for ( i=0; i<n; i++ ) {
printf( "%lf %lf;", (points+indices[i])->x, (points+indices[i])->y);
}
puts("");
return 0;
}
int regular_polygon_svg_line_point_pairs (unsigned n) {
unsigned i, j;
point *points;
for ( i=3; i<=n; i++ ) {
points = regular_polygon_vertices(i);
fprintf(stderr, "Regular Polygon with %d sides\n", i);
choose( i, 2, points, svg_line_point_pair);
free(points);
}
return 0;
}
int print_regular_polygon_point_pairs (unsigned n) {
unsigned i, j;
point *points;
for ( i=3; i<=n; i++ ) {
points = regular_polygon_vertices(i);
printf("Regular Polygon with %d sides\n", i);
for ( j=0; j<i; j++ ) {
permute( i, 2, points, print_points );
}
free(points);
}
return 0;
}
int main( int argc, char **argv) {
unsigned w [56];
regular_polygon_svg_line_point_pairs(14);
print_regular_polygon_points(5);
choose( 5, 2, w, print_indices);
return 0;
}
/*
* PK: I can write powerful code in C too.
* WOAH. This uses a permutator and combinator.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
char *noram="RAM denied\n";
void die (char *msg) {
fputs (msg, stderr);
exit (0);
}
typedef struct {
double x;
double y;
} point;
point * regular_polygon_vertices(unsigned n) {
int i;
double PI = atan2(1,1)*4;
point * out;
if (!(out = malloc(n*sizeof(point)))) die("no RAM alloted\n");
for ( i=0; i<n; i++ ) {
(out+i)->x = cos((PI*2*i)/n);
(out+i)->y = sin((PI*2*i)/n);
}
return out;
}
int print_regular_polygon_points (unsigned n) {
unsigned i, j;
point *points;
for ( i=3; i<=n; i++ ) {
points = regular_polygon_vertices(i);
printf("Regular Polygon with %d sides\n", i);
for ( j=0; j<i; j++ ) {
printf( "%lf %lf\n", (points+j)->x, (points+j)->y);
}
free(points);
}
return 0;
}
int svg_line( point a, point b) {
printf(
"<line x1=\"%lf\" y1=\"%lf\" x2=\"%lf\" y2=\"%lf\" stroke=\"black\" stroke-width=\"0.001\"/>\n",
a.x, a.y, b.x, b.y
);
return 0;
}
int svg_line_point_pair(point *points, unsigned *t, unsigned n) {
assert(n==2);
svg_line( *(points+t[0]), *(points+t[1]) );
return 0;
}
int print_points(point *points, unsigned *t, unsigned n) {
unsigned i;
for ( i=0; i<n; i++ ) {
printf( "%lf %lf;", (points+t[i])->x, (points+t[i])->y);
}
puts("");
return 0;
}
int print(unsigned *s, unsigned *t, unsigned r){
unsigned i;
for(i=0;i<r;i++){
printf("%x",t[i]);
}
putchar('\n');
return 0;
}
int perm(unsigned n, unsigned r, unsigned *s, unsigned* t, unsigned level, void *array, int (*process) (void *array, unsigned *indices, unsigned count)){
unsigned v;
if (level==r) {
(*process)(array, t, r);
return 0;
} else if (level<r) {
for(v=0;v<n;v++) {
if(s[v]) continue;
t[level]=v;
s[v]=1;
perm(n, r, s, t, level+1, array, process);
s[v]=0;
}
} else {
puts("level>r err!\n");
exit(0);
}
}
int permute( unsigned n, unsigned r, void *array, int (*process) (void *array, unsigned *indices, unsigned count) ) {
unsigned *t;
unsigned *s;
if (!(s = (unsigned *)malloc(n*sizeof(unsigned)))) die("no RAM alloted\n");
memset(s,0,n*sizeof(unsigned));
if (!(t = (unsigned *)malloc(r*sizeof(unsigned)))) die("no RAM alloted\n");
perm(n, r, s, t, 0, array, process);
free(t);
free(s);
return 0;
}
int choices( unsigned n, unsigned r, unsigned *choice, unsigned level, void *array, int (*process) (void *array, unsigned *indices, unsigned count)){
while( choice[level] <= level+n-r) {
if (level == r) {
(*process)(array, choice, r);
return 0;
} else {
choice[level+1] = choice[level] + 1;
choices(n, r, choice, level+1, array, process);
}
choice[level]++;
}
return 0; /* dummy meaningless shit */
}
int choose( unsigned n, unsigned r, void *array, int (*process) (void *array, unsigned *indices, unsigned count) ) {
unsigned *choice;
if (r!=0) {
if(!(choice=malloc((r)*sizeof(unsigned)))) die(noram);
*choice = 0;
choices(n, r, choice, 0, array, process);
free(choice);
}
else fputs("\n",stdout);
return 0;
}
int regular_polygon_svg_line_point_pairs (unsigned n) {
unsigned i, j;
point *points;
for ( i=3; i<=n; i++ ) {
points = regular_polygon_vertices(i);
fprintf(stderr, "Regular Polygon with %d sides\n", i);
choose( i, 2, points, svg_line_point_pair);
free(points);
}
return 0;
}
int print_regular_polygon_point_pairs (unsigned n) {
unsigned i, j;
point *points;
for ( i=3; i<=n; i++ ) {
points = regular_polygon_vertices(i);
printf("Regular Polygon with %d sides\n", i);
for ( j=0; j<i; j++ ) {
permute( i, 2, points, print_points );
}
free(points);
}
return 0;
}
int main( int argc, char **argv) {
unsigned d[40];
regular_polygon_svg_line_point_pairs(14);
return 0;
}
/*
* PK: I can write powerful code in C too.
* WOAH. This is a permutator.
*/
/*
* This was dated 17 May 2021
* But may have been finished earlier
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment