Sindbad~EG File Manager

Current Path : /usr/home/beeson/public_html/WebMathXpert/Demos/PolygonDemo/
Upload File :
Current File : //usr/home/beeson/public_html/WebMathXpert/Demos/PolygonDemo/svgPolygon.c

// Code written by ChatGPT to Beeson's requirements 8.30.23
// 9.2.23  removed svg header generation
// 

#include <stdio.h>
#include <string.h>
#include <math.h>
#include "svgPolygon.h"

int svgPolygon(int n, int r, char *svgCode, int buflength) {
    if (n <= 2 || r <= 0 || svgCode == NULL || buflength <= 0) {
        return 1; // Invalid inputs, do nothing
    }

    // Calculate the coordinates of the polygon vertices
    int centerX = r+2;
    int centerY = r+2;
    double angleIncrement = 2 * 3.14159265359 / n; // 2 * pi / n
    sprintf(svgCode, "<svg>\n");

    // Construct SVG code
    /*   Now this is statically in the PHP file
    snprintf(svgCode, buflength,
             "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
             "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n"
             "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
             "<svg width=\"%d\" height=\"%d\" xmlns=\"http://www.w3.org/2000/svg\">\n",
             2 * r+4, 2 * r+4);
    */  
    // Append SVG code for polygon vertices and sides
    for (int i = 0; i < n; ++i) {
        int x = centerX + (int)(r * cos(i * angleIncrement));
        int y = centerY + (int)(r * sin(i * angleIncrement));
        snprintf(svgCode + strlen(svgCode), buflength - strlen(svgCode),
                 "  <circle cx=\"%d\" cy=\"%d\" r=\"2\" fill=\"blue\" />\n", x, y);
        int nextX = centerX + (int)(r * cos((i + 1) * angleIncrement));
        int nextY = centerY + (int)(r * sin((i + 1) * angleIncrement));
        snprintf(svgCode + strlen(svgCode), buflength - strlen(svgCode),
                 "  <line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke=\"black\" />\n",
                 x, y, nextX, nextY);
    }

    // Close SVG tag
    strncat(svgCode, "</svg>\n", buflength - strlen(svgCode)-1);
	 return 0;
}

/*
int main() {
    char svgBuffer[2048]; // Buffer to hold generated SVG code
    int n = 6; // Number of sides
    int r = 100; // Radius

    svgPolygon(n, r, svgBuffer, sizeof(svgBuffer));
    printf("%s", svgBuffer);

    return 0;
}
*/


Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists