Skip to content

Instantly share code, notes, and snippets.

@livibetter
Last active June 29, 2021 22:07
Show Gist options
  • Save livibetter/63843c2667f2c61f91d6 to your computer and use it in GitHub Desktop.
Save livibetter/63843c2667f2c61f91d6 to your computer and use it in GitHub Desktop.
A rotating ring

ring.c

A rotating ring.

https://lh3.googleusercontent.com/-X-6j8HQRpak/VfzunKdCMlI/AAAAAAAAH4I/XDZ8cwwE9s4/s288-Ic42/ring.c.gif https://lh3.googleusercontent.com/-oJqEVFthZOo/Vfz55-6FbjI/AAAAAAAAH4k/MaDQid2xXuY/s288-Ic42/ring.c.noclear.gif

This is a byproduct of something else I am trying to code, it could be better with randomness, multiple rings, ellipses, rotations, color gradients, et cetera. I may or may not come back to finish this, but most likely not unless someone is willing to join force to finish this together. Contact me if you want to code this, or you can clone this and work on your own.

key function
c clear before updating
s sweep radius between 0 and 0.5
a non-sweep only, increase radius
z non-sweep only, decrease radius
q quit

The contents in this repository have been place into public domain via Unlicense.

PROGRAM = ring
SOURCES = ring.c
CC ?= gcc
CFLAGS ?= -g -O2
CFLAGS += -std=gnu99 -Wall -Wextra -pedantic
LDLIBS += -lm
LDLIBS += $(shell ncursesw5-config --cflags --libs)
INSTALL = install
INSTALL_BIN = $(INSTALL) -D -m 755
INSTALL_MAN = $(INSTALL) -D -m 644
PREFIX = /usr/local
bin_dir = $(PREFIX)/bin
all: $(PROGRAM)
.PHONY: all
$(PROGRAM): $(SOURCES)
clean:
$(RM) *.o
.PHONY: clean
clobber: clean
$(RM) $(PROGRAM)
.PHONY: clobber
install: $(PROGRAM)
$(INSTALL_BIN) $(PROGRAM) $(DESTDIR)$(bin_dir)/$(PROGRAM)
.PHONY: install
install-strip:
$(MAKE) INSTALL_BIN='$(INSTALL_BIN) -s' install
.PHONY: install-strip
uninstall:
$(RM) $(DESTDIR)$(bin_dir)/$(PROGRAM)
.PHONY: uninstall
// ring.c, a rotating ring
// Written by Yu-Jie Lin
//
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>
#include <curses.h>
#include <math.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#define M_2PI (2 * M_PI)
#define SCALE_X (2)
double R, STEP_A;
int SCREEN_H, SCREEN_W;
int CENTER_X, CENTER_Y;
void signal_handler(int sig);
void cleanup(void);
void resize(void);
int
main(void)
{
signal(SIGINT, signal_handler);
initscr();
keypad(stdscr, TRUE);
nonl();
cbreak();
noecho();
timeout(0);
curs_set(0);
if (has_colors())
{
start_color();
for (int i = 0; i < 24; i++)
{
init_pair(i + 1, 232 + i, COLOR_BLACK);
}
}
resize();
bool do_clear = TRUE;
bool do_sweep = TRUE;
bool inward = TRUE;
double r = 0.5;
double a = 0;
struct timespec req = {
.tv_sec = 0,
.tv_nsec = 50000000
};
while (TRUE)
{
int c = getch();
switch (c)
{
case KEY_RESIZE:
resize();
break;
case 'c':
do_clear = !do_clear;
break;
case 's':
do_sweep = !do_sweep;
break;
case 'a':
case 'z':
if (!do_sweep)
{
r += ((c == 'a') ? 1 : -1) / R / SCALE_X;
}
break;
case 'q':
case 'Q':
cleanup();
return EXIT_SUCCESS;
}
if (do_clear)
{
erase();
}
for (double i = 0; i < M_2PI; i += STEP_A)
{
double y, x;
y = r * sin(a + i) * R;
x = r * cos(a + i) * R;
y = CENTER_Y - y;
x = CENTER_X + x * SCALE_X;
attrset(COLOR_PAIR(1 + (int) (i / M_2PI * 360) % 24));
mvaddch(y, x, '*');
}
a += STEP_A;
if (do_sweep)
{
r += (inward ? -1 : 1) / R / SCALE_X;
if (r <= 0)
{
r = 0;
inward = !inward;
}
else if (r >= 0.5)
{
r = 0.5;
inward = !inward;
}
}
nanosleep(&req, NULL);
}
}
void
signal_handler(int sig)
{
cleanup();
signal(sig, SIG_DFL);
raise(sig);
}
void
cleanup(void)
{
endwin();
}
void
resize(void)
{
getmaxyx(stdscr, SCREEN_H, SCREEN_W);
R = sqrt(pow(SCREEN_H / 2, 2) + pow(SCREEN_W / 2 / SCALE_X, 2));
CENTER_X = SCREEN_W / 2;
CENTER_Y = SCREEN_H / 2;
STEP_A = tan((double) 1 / (CENTER_X / SCALE_X));
clear();
}
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment