Last active
September 26, 2022 15:20
-
-
Save oxUnd/29e9842bdf8fde7fdac3e719610f79e6 to your computer and use it in GitHub Desktop.
Draw sin.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <math.h> | |
typedef struct { | |
int x, y; | |
} P; | |
typedef struct { | |
int w, h; | |
} B; | |
const float PI = 3.1415926; | |
const B box = {.w = 128, .h = 32 }; | |
enum { NO = 0, YES = 1 }; | |
int art(int x, int y, const int n) { | |
P p = { .x = x, .y = 0 }; | |
int t = n; | |
if (n < 1) { | |
t = 1; | |
} | |
p.y = sin((double)p.x/(box.w * 1 / t) * PI) * box.h / 2 + box.h / 2; | |
if (p.y == y) { | |
return YES; | |
} | |
return NO; | |
} | |
void draw(const int n) { | |
for (int j = 0; j < box.h; j++) { | |
for (int i = 0; i < box.w; i++) { | |
if (art(i, j, n)) { | |
putchar('x'); | |
} else { | |
if (j == box.h / 2) { | |
putchar('-'); | |
} else { | |
putchar(' '); | |
} | |
} | |
} | |
putchar('\n'); | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int step = 1; | |
scanf("%d", &step); | |
draw(step); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <math.h> | |
typedef struct { | |
int x, y; | |
} P; | |
typedef struct { | |
int w, h; | |
} B; | |
const B box = {.w = 32, .h = 32 }; | |
enum { NO = 0, YES = 1 }; | |
int art(int x, int y) { | |
P p = { .x = x, .y = 0 }; | |
p.y = sin((double)p.x/(box.w * 0.5) * 3.1415926) * box.h / 2 + box.h / 2; | |
if (p.y == y) { | |
return YES; | |
} | |
return NO; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
for (int j = 0; j < box.h; j++) { | |
for (int i = 0; i < box.w; i++) { | |
if (art(i, j)) { | |
putchar('x'); | |
} else { | |
putchar(' '); | |
} | |
if (j == box.h / 2) { | |
putchar('-'); | |
} | |
} | |
putchar('\n'); | |
} | |
return 0; | |
} |
Author
oxUnd
commented
Sep 23, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment