Skip to content

Instantly share code, notes, and snippets.

@paeryn
Created April 9, 2022 11:06
Show Gist options
  • Save paeryn/ad473c4f36a692398dbf1e19a68fa1c7 to your computer and use it in GitHub Desktop.
Save paeryn/ad473c4f36a692398dbf1e19a68fa1c7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "VG/openvg.h"
#include "VG/vgu.h"
#include "fontinfo.h"
#include "shapes.h"
#include <math.h>
void MakeArc(VGfloat x, VGfloat y, VGfloat r, VGfloat sa, VGfloat ext,
VGubyte **segments, int *seg_size,
VGfloat **coordinates, int *coord_size)
{
VGfloat xpos(VGfloat x, VGfloat r, VGfloat a)
{
VGfloat t = a * M_PI / 180.0f;
return x + r * cosf(t);
}
VGfloat ypos(VGfloat y, VGfloat r, VGfloat a)
{
VGfloat t = a * M_PI / 180.0f;
return y + r * sinf(t);
}
const int divs = 20;
VGubyte *segs = malloc(sizeof(VGubyte) * (divs + 1));
VGfloat *coords = malloc(sizeof(VGfloat) * (divs + 1) * 2);
segs[0] = VG_MOVE_TO;
coords[0] = xpos(x, r, sa); coords[1] = ypos(y, r, sa);
int ss = 1, cs = 2;
for (int i = 0; i < divs; i++) {
sa += ext / divs;
if (sa < 0.0f) sa += 360.0f;
else if (sa >= 360.0f) sa -= 360.0f;
segs[ss++] = VG_LINE_TO;
coords[cs++] = xpos(x, r, sa);
coords[cs++] = ypos(y, r, sa);
}
*segments = segs;
*coordinates = coords;
*seg_size = ss;
*coord_size = cs;
}
void DrawObj(VGubyte *inSegments, int inSegments_size,
VGfloat *inCoords, int inCoords_size,
VGubyte *outSegments, int outSegments_size,
VGfloat *outCoords, int outCoords_size)
{
int AllSegments_size = outSegments_size + 1 + 1 + 1 + inSegments_size;
int AllCoords_size = outCoords_size + 2 + 2 + 2 + inCoords_size;
VGubyte *AllSegments = (VGubyte*)malloc(sizeof(VGubyte) * AllSegments_size);
VGfloat *AllCoords = (VGfloat*)malloc(sizeof(VGfloat) * AllCoords_size);
int AllSegments_index = 0;
int AllCoords_index = 0;
for (int i = 0; i < outSegments_size; i ++) {
AllSegments[AllSegments_index] = outSegments[i];
AllSegments_index ++;
}
for (int j = 0; j < outCoords_size; j ++) {
AllCoords[AllCoords_index] = outCoords[j];
AllCoords_index ++;
}
inSegments[0] = VG_LINE_TO;
for (int i = 0; i < inSegments_size; i ++) {
AllSegments[AllSegments_index] = inSegments[i];
AllSegments_index ++;
}
for (int j = 0; j < inCoords_size; j ++) {
AllCoords[AllCoords_index] = inCoords[j];
AllCoords_index ++;
}
AllSegments[AllSegments_index] = VG_CLOSE_PATH;
VGPath path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_APPEND_TO);
vgAppendPathData(path, AllSegments_size, AllSegments, AllCoords);
vgRemovePathCapabilities(path, VG_PATH_CAPABILITY_APPEND_FROM | VG_PATH_CAPABILITY_APPEND_TO |
VG_PATH_CAPABILITY_MODIFY | VG_PATH_CAPABILITY_TRANSFORM_FROM |
VG_PATH_CAPABILITY_TRANSFORM_TO | VG_PATH_CAPABILITY_INTERPOLATE_FROM |
VG_PATH_CAPABILITY_INTERPOLATE_TO);
free(AllSegments);
free(AllCoords);
vgDrawPath(path, VG_STROKE_PATH | VG_FILL_PATH);
vgDestroyPath(path);
}
int main() {
int width, height;
char s[3];
init(&width, &height);
Start(width, height);
Background(0, 0, 0);
Fill(44, 77, 232, 1);
Stroke(255, 255, 255, 1);
StrokeWidth(2);
VGubyte *outSeg, *inSeg;
VGfloat *outCoord, *inCoord;
int outSeg_size, inSeg_size, outCoord_size, inCoord_size;
MakeArc(width / 2.0f, height / 2.0f, height / 4.0f, 200.0f, -160.0f, &outSeg, &outSeg_size, &outCoord, &outCoord_size);
VGPath outerArc = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, outSeg_size, outCoord_size, VG_PATH_CAPABILITY_APPEND_TO);
vgAppendPathData(outerArc, outSeg_size, outSeg, outCoord);
MakeArc(width / 2.0f , height / 2.0f, height / 4.5f, 40.0f, 160.0f, &inSeg, &inSeg_size, &inCoord, &inCoord_size);
VGPath inArc = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, inSeg_size, inCoord_size, VG_PATH_CAPABILITY_APPEND_TO);
vgAppendPathData(inArc, inSeg_size, inSeg, inCoord);
DrawObj(inSeg, inSeg_size, inCoord, inCoord_size, outSeg,
outSeg_size, outCoord, outCoord_size);
// vgDrawPath(outerArc, VG_STROKE_PATH);
// vgDrawPath(inArc, VG_STROKE_PATH);
End();
fgets(s, 2, stdin);
finish();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment