commit 9adfdba838437fe8667d6dd38daa747370dbd4c8 Author: ALittlePatate Date: Sat Sep 3 13:18:07 2022 +0200 feet : initial commit of all the files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5d37a9c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.d +*.o +*.exe +.vscode \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5d253a9 --- /dev/null +++ b/Makefile @@ -0,0 +1,69 @@ +######################################################################## +####################### Makefile Template ############################## +######################################################################## + +# Compiler settings - Can be customized. +CC = gcc +CXXFLAGS = -std=c11 -Wall -O2 +LDFLAGS = + +# Makefile settings - Can be customized. +APPNAME = poly +EXT = .c +SRCDIR = . +OBJDIR = . + +############## Do not change anything from here downwards! ############# +SRC = $(wildcard $(SRCDIR)/*$(EXT)) +OBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)/%.o) +DEP = $(OBJ:$(OBJDIR)/%.o=%.d) +# UNIX-based OS variables & settings +RM = rm +DELOBJ = $(OBJ) +# Windows OS variables & settings +DEL = del +EXE = .exe +WDELOBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)\\%.o) + +######################################################################## +####################### Targets beginning here ######################### +######################################################################## + +all: $(APPNAME) + +# Builds the app +$(APPNAME): $(OBJ) + $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) + +# Creates the dependecy rules +%.d: $(SRCDIR)/%$(EXT) + @$(CPP) $(CFLAGS) $< -MM -MT $(@:%.d=$(OBJDIR)/%.o) >$@ + +# Includes all .h files +-include $(DEP) + +# Building rule for .o files and its .c/.cpp in combination with all .h +$(OBJDIR)/%.o: $(SRCDIR)/%$(EXT) + $(CC) $(CXXFLAGS) -o $@ -c $< + +################### Cleaning rules for Unix-based OS ################### +# Cleans complete project +.PHONY: clean +clean: + $(RM) $(DELOBJ) $(DEP) $(APPNAME) + +# Cleans only all files with the extension .d +.PHONY: cleandep +cleandep: + $(RM) $(DEP) + +#################### Cleaning rules for Windows OS ##################### +# Cleans complete project +.PHONY: cleanw +cleanw: + $(DEL) $(WDELOBJ) $(DEP) $(APPNAME)$(EXE) + +# Cleans +.PHONY: cleandepw +cleandepw: all + $(DEL) $(DEP) $(WDELOBJ) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0a0e723 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# 2D Polynomials +I was bored so i made this tool, it allows you to calculate : +* the delta +* x1, x2, x0 +* vertex coordinates + +The names of the functions/variables are in French, uhh use google translate to read the code i guess, sorry for that. + +# Usage +```poly.exe a b c ``` + +# Screenshot +![Screenshot](screenshot/screenshot.png) \ No newline at end of file diff --git a/polynome.c b/polynome.c new file mode 100644 index 0000000..1a28f51 --- /dev/null +++ b/polynome.c @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include "polynome.h" + +enum STATUS Calcul_Delta(Poly* poly) { + if (poly == NULL) { + return FAIL; + } + + poly->delta = (poly->b * poly->b) - (4 * poly->a * poly->c); + return OK; +} + +enum STATUS Calcul_x1_x2(Poly* poly) { + if (poly == NULL) { + return FAIL; + } + + poly->x1 = (-poly->b - sqrt(poly->delta)) / (2 * poly->a); + poly->x2 = (-poly->b + sqrt(poly->delta)) / (2 * poly->a); + + return OK; +} + +enum STATUS Calcul_x0(Poly* poly) { + if (poly == NULL) { + return FAIL; + } + + poly->x0 = -poly->b / (2 * poly->a); + + return OK; +} + +enum STATUS Calcul_Sommet(Poly* poly) { + if (poly == NULL) { + return FAIL; + } + + poly->sx = -poly->b / (2 * poly->a); + + float result; + if (Calcul_Fonction(poly, poly->sx, &result) == FAIL) { + return FAIL; + } + + poly->sy = result; + + return OK; +} + +enum STATUS Calcul_Fonction(Poly* poly, float x, float *out) { + if (poly == NULL) { + return FAIL; + } + + *out = poly->a * x * x + poly->b * x + poly->c; + return OK; +} + +enum STATUS Dessiner_Graph(Poly* poly) { + if (poly == NULL) { + return FAIL; + } + + printf("\n"); + for (int j = LONGUEUR_Y; j >= 1; j--) { + + if (j == 10) { + printf("%d | ",j); + } + else { + printf("%d | ",j); + } + + for (int i = 1; i <= LONGUEUR_X; i++) { + float y; + if (Calcul_Fonction(poly, i, &y) == FAIL) { + return FAIL; + } + + if (roundf(y) == j) { + printf("o "); + } + else { + printf(" "); + } + } + printf("\n"); + } + + // | 1 2 3 4 5 6 7 8 9 10 + printf("----+---------------------\n"); + printf(" | "); + for (int k = 1; k <= LONGUEUR_Y; k++) { + printf("%d ",k); + } + printf("\n----+---------------------\n"); + + for (int j = 0; j >= -LONGUEUR_Y; j--) { + + if (j == -10) { + printf("%d | ",j); + } + else if (j == 0) { + printf("%d | ",j); + } + else { + printf("%d | ",j); + } + + for (int i = 1; i <= LONGUEUR_X; i++) { + float y; + if (Calcul_Fonction(poly, i, &y) == FAIL) { + return FAIL; + } + + if (roundf(y) == j) { + printf("o "); + } + else { + printf(" "); + } + } + printf("\n"); + } + + printf("\n"); + return OK; +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + fprintf(stderr, "%s", "[-] usage : poly.exe a b c\n"); + return 1; + } + + float a = atof(argv[1]); + float b = atof(argv[2]); + float c = atof(argv[3]); + + printf("[+] Processing %.3fx^2%.3fx%.3f\n", a, b, c); + + Poly* poly = malloc(sizeof(Poly)); + poly->a = a; + poly->b = b; + poly->c = c; + + if (Calcul_Delta(poly) == FAIL) { + fprintf(stderr, "%s", "[-] Error while calculating delta.\n"); + } + else { + printf("[+] Delta : %.3f\n",poly->delta); + } + + if (poly->delta > 0) { + printf("[+] The polynomial admits 2 solutions x1 and x2.\n"); + + if (Calcul_x1_x2(poly) == OK) { + printf("[+] x1 : %.4f\n",poly->x1); + printf("[+] x2 : %.4f\n",poly->x2); + } + else { + fprintf(stderr, "%s", "[-] Error while calculating x1 or x2.\n"); + } + } + else if (poly->delta == 0) { + printf("[+] The polynomial admits 1 solution x0\n"); + + if (Calcul_x0(poly) == OK) { + printf("[+] x0 : %.4f\n",poly->x0); + } + else { + fprintf(stderr, "%s", "[-] Error while calculating x0.\n"); + } + } + else { + printf("[+] The polynomial has no solution\n"); + } + + if (Calcul_Sommet(poly) == FAIL) { + fprintf(stderr, "%s", "[-] Error while calculating vertex coordinates.\n"); + } + else { + printf("[+] Vertex coordinates : S = (%.4f,%.4f)\n",poly->sx,poly->sy); + } + + printf("[+] Drawing the graph...\n"); + if (Dessiner_Graph(poly) == FAIL) { + fprintf(stderr, "%s", "[-] Error while drawing the graph.\n"); + } + + free(poly); + return 0; +} \ No newline at end of file diff --git a/polynome.h b/polynome.h new file mode 100644 index 0000000..9dab2b8 --- /dev/null +++ b/polynome.h @@ -0,0 +1,30 @@ +typedef struct Poly { + float a; + float b; + float c; + + float delta; + + float sx; //sommet x + float sy; //sommet y + + float x1; + float x2; + float x0; +} Poly; + +enum STATUS { + OK, + FAIL +}; + +enum STATUS Calcul_Delta(Poly* poly); +enum STATUS Calcul_x1_x2(Poly* poly); +enum STATUS Calcul_x0(Poly* poly); +enum STATUS Calcul_Sommet(Poly* poly); +enum STATUS Calcul_Fonction(Poly* poly, float x, float *out); + +#define LONGUEUR_X 10 +#define LONGUEUR_Y 10 +enum STATUS Dessiner_Graph(Poly* poly); +int main(); \ No newline at end of file diff --git a/screenshot/screenshot.png b/screenshot/screenshot.png new file mode 100644 index 0000000..639c7e2 Binary files /dev/null and b/screenshot/screenshot.png differ