diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000000000000000000000000000000000..84c1ea5bdbc24d714cbc7666bba7fc498ace5070 --- /dev/null +++ b/.clang-format @@ -0,0 +1,178 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveMacros: None +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: None +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: true +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseLabels: false +IndentCaseBlocks: false +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentRequires: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PenaltyIndentedWhitespace: 0 +PointerAlignment: Right +PPIndentWidth: -1 +ReferenceAlignment: Pointer +ReflowComments: true +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceAroundPointerQualifiers: Default +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +BitFieldColonSpacing: Both +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE + - NS_SWIFT_NAME + - CF_SWIFT_NAME +--- + diff --git a/charge/charge.c b/charge/charge.c index 59de8aac46225b6d9b238d65a413a314d2b5d1df..b047e1b989dd74e8eb1f7b4b81307cd56a90b71a 100644 --- a/charge/charge.c +++ b/charge/charge.c @@ -4,17 +4,16 @@ * @brief Chrage library * @version 0.1 * @date 2021-07-12 - * + * * @copyright Copyright (c) 2021 - * + * */ +#include "charge.h" #include "../draw/draw.h" -#include "../vec2/vec2.h" #include "../utilities/utils.h" -#include "charge.h" +#include "../vec2/vec2.h" -bool compute_e(charge_t c, vec2 p, double eps, vec2 *e) -{ +bool compute_e(charge_t c, vec2 p, double eps, vec2 *e) { vec2 qP = vec2_sub(p, c.pos); double norm = vec2_norm(qP); double E = K * c.q / pow(norm, 2); // E = k * (Q / r^2) @@ -22,13 +21,12 @@ bool compute_e(charge_t c, vec2 p, double eps, vec2 *e) return (norm > eps); } -bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, double eps, vec2 *e) -{ +bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, + double eps, vec2 *e) { vec2 sum; vec2 tmp_e; - for (int i = 0; i < num_charges; i++) - { + for (int i = 0; i < num_charges; i++) { if (!compute_e(charges[i], p, eps, &tmp_e)) return false; sum = vec2_add(sum, tmp_e); @@ -38,10 +36,11 @@ bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, doub return true; } -bool compute_p_next(charge_t *charges, int num_charges, double eps, double delta, vec2 *p, bool opposed) -{ +bool compute_p_next(charge_t *charges, int num_charges, double eps, + double delta, vec2 *p, bool opposed) { vec2 v; - bool can_draw = compute_total_normalized_e(charges, num_charges, *p, eps, &v); + bool can_draw = + compute_total_normalized_e(charges, num_charges, *p, eps, &v); v = vec2_div(vec2_mul(v, delta), vec2_norm(v)); @@ -53,15 +52,13 @@ bool compute_p_next(charge_t *charges, int num_charges, double eps, double delta return can_draw; } -bool pos_contrain_in_universe(vec2 pos, double x0, double x1, double y0, double y1) -{ +bool pos_contrain_in_universe(vec2 pos, double x0, double x1, double y0, + double y1) { return (pos.x <= x1 && pos.x >= x0 && pos.y <= y1 && pos.y >= y0); } -bool pos_not_too_close(vec2 p, charge_t *c, int num_charges) -{ - for (int i = 0; i < num_charges; i++) - { +bool pos_not_too_close(vec2 p, charge_t *c, int num_charges) { + for (int i = 0; i < num_charges; i++) { double norm = vec2_norm(vec2_sub(p, c[i].pos)); if (norm < EPS) return false; @@ -69,12 +66,15 @@ bool pos_not_too_close(vec2 p, charge_t *c, int num_charges) return true; } -bool field_line_segment(struct gfx_context_t *ctxt, charge_t *charges, int num_charges, double dx, double x0, double x1, double y0, double y1, vec2 old, vec2 p, bool side) -{ - while (pos_contrain_in_universe(p, x0, x1, y0, y1) && pos_not_too_close(p, charges, num_charges)) - { - coordinates_t old_coord = position_to_coordinates(ctxt->width, ctxt->height, x0, x1, y0, y1, old); - coordinates_t pi_coord = position_to_coordinates(ctxt->width, ctxt->height, x0, x1, y0, y1, p); +bool field_line_segment(struct gfx_context_t *ctxt, charge_t *charges, + int num_charges, double dx, double x0, double x1, + double y0, double y1, vec2 old, vec2 p, bool side) { + while (pos_contrain_in_universe(p, x0, x1, y0, y1) && + pos_not_too_close(p, charges, num_charges)) { + coordinates_t old_coord = position_to_coordinates( + ctxt->width, ctxt->height, x0, x1, y0, y1, old); + coordinates_t pi_coord = position_to_coordinates( + ctxt->width, ctxt->height, x0, x1, y0, y1, p); gfx_draw_line(ctxt, pi_coord, old_coord, 0xFFFFFF); @@ -85,22 +85,23 @@ bool field_line_segment(struct gfx_context_t *ctxt, charge_t *charges, int num_c return true; } -void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int num_charges, double dx, vec2 pos0, double x0, double x1, double y0, double y1) -{ +void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, + int num_charges, double dx, vec2 pos0, double x0, + double x1, double y0, double y1) { vec2 p = pos0; vec2 old = p; bool ended = false; - for (int side = 0; side <= 1; side++) - { + for (int side = 0; side <= 1; side++) { old = pos0; // Reset to start from the same point for both sides - field_line_segment(ctxt, charges, num_charges, dx, x0, x1, y0, y1, old, p, (bool)side); + field_line_segment(ctxt, charges, num_charges, dx, x0, x1, y0, y1, old, + p, (bool)side); } } -void draw_charges(struct gfx_context_t *ctxt, charge_t *charges, int num_charges, double x0, double x1, double y0, double y1) -{ +void draw_charges(struct gfx_context_t *ctxt, charge_t *charges, + int num_charges, double x0, double x1, double y0, double y1) { // Center coordinates coordinates_t c; @@ -111,32 +112,40 @@ void draw_charges(struct gfx_context_t *ctxt, charge_t *charges, int num_charges int chargePadding = CHARGE_RADIUS / 2; // Draw charges - for (int i = 0; i < num_charges; i++) - { - coordinates_t chCoord = position_to_coordinates(ctxt->width, ctxt->height, x0, x1, y0, y1, charges[i].pos); + for (int i = 0; i < num_charges; i++) { + coordinates_t chCoord = position_to_coordinates( + ctxt->width, ctxt->height, x0, x1, y0, y1, charges[i].pos); // Draw sign according to charge value - if (charges[i].q < 0) - { - gfx_draw_line(ctxt, coordinates_create(chCoord.row, chCoord.column - chargePadding), coordinates_create(chCoord.row, chCoord.column + chargePadding), COLOR_BLUE); - } - else - { - gfx_draw_line(ctxt, coordinates_create(chCoord.row, chCoord.column - chargePadding), coordinates_create(chCoord.row, chCoord.column + chargePadding), COLOR_RED); - gfx_draw_line(ctxt, coordinates_create(chCoord.row - chargePadding, chCoord.column), coordinates_create(chCoord.row + chargePadding, chCoord.column), COLOR_RED); + if (charges[i].q < 0) { + gfx_draw_line( + ctxt, + coordinates_create(chCoord.row, chCoord.column - chargePadding), + coordinates_create(chCoord.row, chCoord.column + chargePadding), + COLOR_BLUE); + } else { + gfx_draw_line( + ctxt, + coordinates_create(chCoord.row, chCoord.column - chargePadding), + coordinates_create(chCoord.row, chCoord.column + chargePadding), + COLOR_RED); + gfx_draw_line( + ctxt, + coordinates_create(chCoord.row - chargePadding, chCoord.column), + coordinates_create(chCoord.row + chargePadding, chCoord.column), + COLOR_RED); } - gfx_draw_circle(ctxt, chCoord, CHARGE_RADIUS, charges[i].q < 0 ? COLOR_BLUE : COLOR_RED); + gfx_draw_circle(ctxt, chCoord, CHARGE_RADIUS, + charges[i].q < 0 ? COLOR_BLUE : COLOR_RED); } } -void generate_points(vec2 points[], int nb_points) -{ +void generate_points(vec2 points[], int nb_points) { double x = 0; double y = 0; - for (int i = 0; i < nb_points; i++) - { + for (int i = 0; i < nb_points; i++) { x = rand_one(); y = rand_one(); diff --git a/charge/charge.h b/charge/charge.h index 60d6d25b9788a43b28fa902624c941dffe45a90b..333ad4e59e96adca935d2114cd159fd251608152 100644 --- a/charge/charge.h +++ b/charge/charge.h @@ -4,9 +4,9 @@ * @brief Charge file header * @version 0.1 * @date 2021-07-12 - * + * * @copyright Copyright (c) 2021 - * + * */ #ifndef _CHARGE_H_ #define _CHARGE_H_ @@ -15,15 +15,15 @@ #define CHARGE_RADIUS 10 #define EPS 0.025 -#include <stdlib.h> -#include <stdbool.h> +#include "../gfx/gfx.h" #include "../utilities/utils.h" #include "../vec2/vec2.h" -#include "../gfx/gfx.h" +#include <stdbool.h> +#include <stdlib.h> /** * @brief Compute E*qP/norm(qP) - * + * * @param c Parent charge * @param p Point on which to compute e * @param eps Minimal gap between field and charge @@ -34,7 +34,7 @@ bool compute_e(charge_t c, vec2 p, double eps, vec2 *e); /** * @brief Compute the normilized sum of Ei*qiP/norm(qiP) - * + * * @param charges All the charges * @param num_charges Number of charges in action * @param p Current computed point @@ -47,21 +47,22 @@ bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, /** * @brief Compute the next particle - * + * * @param charges All the charges * @param num_charges Number of charges in action * @param eps Minimal gap between field and charge * @param delta Distance between two points in the field line * @param p Current point * @param opposed Is the next point towards charge - * @return true - * @return false + * @return true + * @return false */ -bool compute_p_next(charge_t *charges, int num_charges, double eps, double delta, vec2 *p, bool opposed); +bool compute_p_next(charge_t *charges, int num_charges, double eps, + double delta, vec2 *p, bool opposed); /** * @brief Draw a segment of a field line between two given points - * + * * @param ctxt Context to draw on * @param charges All the charges * @param num_charges Number of charges @@ -73,14 +74,16 @@ bool compute_p_next(charge_t *charges, int num_charges, double eps, double delta * @param old Old pos * @param p Next pos * @param side Side of the line - * @return true - * @return false + * @return true + * @return false */ -bool field_line_segment(struct gfx_context_t *ctxt, charge_t *charges, int num_charges, double dx, double x0, double x1, double y0, double y1, vec2 old, vec2 p, bool side); +bool field_line_segment(struct gfx_context_t *ctxt, charge_t *charges, + int num_charges, double dx, double x0, double x1, + double y0, double y1, vec2 old, vec2 p, bool side); /** * @brief Draw a field line - * + * * @param ctxt Context to draw on * @param charges All the charges in action * @param num_charges Number of charges @@ -91,13 +94,15 @@ bool field_line_segment(struct gfx_context_t *ctxt, charge_t *charges, int num_c * @param y0 Minimal Y * @param y1 Maximal Y */ -void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int num_charges, double dx, vec2 pos0, double x0, double x1, double y0, double y1); +void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, + int num_charges, double dx, vec2 pos0, double x0, + double x1, double y0, double y1); /** * @brief Draw all charges * A circle with a minus sign for negative charges * A circle with a plus sign for positive charges - * + * * @param ctxt Context to draw on * @param charges All the charges * @param num_charges Number of charges in action @@ -111,7 +116,7 @@ void draw_charges(struct gfx_context_t *ctxt, charge_t *charges, /** * @brief Generate a given amount of points - * + * * @param points Points array * @param nb_points Number of point to generate */ diff --git a/draw/draw.c b/draw/draw.c index 24368a1381e471f0c627f45737daf668cf27b7d2..11b5b5e2614ac977e35c1bb581604d51f210983d 100644 --- a/draw/draw.c +++ b/draw/draw.c @@ -4,23 +4,22 @@ * @brief Drawing library * @version 0.1 * @date 2021-07-12 - * + * * @copyright Copyright (c) 2021 - * + * */ -#include <math.h> #include "draw.h" +#include <math.h> /** * @brief Create a coordinate - * + * * @param row_ Row number - * @param column_ Column number - * @return coordinates_t + * @param column_ Column number + * @return coordinates_t */ -coordinates_t coordinates_create(int row_, int column_) -{ +coordinates_t coordinates_create(int row_, int column_) { coordinates_t c = {.row = row_, .column = column_}; return c; } @@ -30,13 +29,14 @@ coordinates_t coordinates_create(int row_, int column_) * Full understanding of the algorithme, but some parts of the * implementation still blury. * @see https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases - * + * * @param ctxt Context to draw on * @param p0 Start point * @param p1 End point * @param color Stroke color */ -void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p1, uint32_t color) { +void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, + coordinates_t p1, uint32_t color) { int x0 = p0.column, y0 = p0.row; int x1 = p1.column, y1 = p1.row; @@ -47,9 +47,11 @@ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p } int dx = abs(x1 - x0); - int sx = x0 < x1 ? 1 : -1; // Increase if x goes left to right and decrease if right to left + // Increase if x goes left to right and decrease if right to left + int sx = x0 < x1 ? 1 : -1; int dy = -abs(y1 - y0); - int sy = y0 < y1 ? 1 : -1; // Increase if y goes up to down and decrease if down to up + // Increase if y goes up to down and decrease if down to ups + int sy = y0 < y1 ? 1 : -1; int err = dx + dy; // Error value between x and y int e2; @@ -57,7 +59,8 @@ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p while (true) { gfx_putpixel(ctxt, x0, y0, color); - if (x0 == x1 && y0 == y1) break; + if (x0 == x1 && y0 == y1) + break; e2 = 2 * err; @@ -74,14 +77,16 @@ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p /** * @brief Andres's circle algorithm - * @see https://fr.wikipedia.org/wiki/Algorithme_de_tracé_de_cercle_d%27Andres#Algorithme - * + * @see + * https://fr.wikipedia.org/wiki/Algorithme_de_tracé_de_cercle_d%27Andres#Algorithme + * * @param ctxt Context to draw on * @param c Center * @param r Radius * @param color Stroke color */ -void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r, uint32_t color) { +void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r, + uint32_t color) { int xc = c.column; int yc = c.row; int x = 0; diff --git a/draw/draw.h b/draw/draw.h index 5f372073b6b1bebb80d5443e7d2a1d83d7a00861..53adfa430cd30f63b29ba3bbe1680fe995024f04 100644 --- a/draw/draw.h +++ b/draw/draw.h @@ -4,52 +4,53 @@ * @brief Header of the draw file * @version 0.1 * @date 2021-07-12 - * + * * @copyright Copyright (c) 2021 - * + * */ #ifndef _DRAW_H_ #define _DRAW_H_ -#include <stdlib.h> -#include <stdio.h> -#include <stdint.h> #include "../gfx/gfx.h" +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> -typedef struct -{ +typedef struct { uint32_t row; uint32_t column; } coordinates_t; /** * @brief Create a coordinate - * + * * @param row_ Row number * @param column_ Column number - * @return coordinates_t + * @return coordinates_t */ coordinates_t coordinates_create(int row_, int column_); /** * @brief Draw a line using the Bresenham's line algorithm - * + * * @param ctxt Context to draw on * @param p0 Start * @param p1 End * @param color Stroke color */ -void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p1, uint32_t color); +void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, + coordinates_t p1, uint32_t color); /** * @brief Draw a circle using _______ - * + * * @param ctxt Context to draw on * @param c Center * @param r Raidus * @param color Stroke color */ -void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r, uint32_t color); +void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r, + uint32_t color); #endif // _DRAW_H_ diff --git a/draw/test.c b/draw/test.c index 365e491ec99a6bf1add1863a39b384e211df3ec7..95b1ec9799b6a7327951f2b780aa33cda2f9ca7d 100644 --- a/draw/test.c +++ b/draw/test.c @@ -4,19 +4,20 @@ * @brief Test file for the draw functions * @version 0.1 * @date 2021-07-12 - * + * * @copyright Copyright (c) 2021 - * + * */ -#include <stdlib.h> #include "../gfx/gfx.h" #include "draw.h" +#include <stdlib.h> int main(void) { const int WINDOW_WIDTH = 100; const int WINDOW_HEIGHT = 100; - struct gfx_context_t *ctxt = gfx_create("draw", WINDOW_WIDTH, WINDOW_HEIGHT); + struct gfx_context_t *ctxt = + gfx_create("draw", WINDOW_WIDTH, WINDOW_HEIGHT); if (!ctxt) { fprintf(stderr, "Graphics initialization failed !\n"); @@ -52,7 +53,7 @@ int main(void) { gfx_draw_line(ctxt, p0, p11, COLOR_WHITE); gfx_draw_line(ctxt, p0, p12, COLOR_WHITE); - while (gfx_keypressed() != SDLK_ESCAPE){ + while (gfx_keypressed() != SDLK_ESCAPE) { gfx_present(ctxt); } diff --git a/main.c b/main.c index 73c5fd2cde6c053dcfba1f140302aa4acf9c77fe..4395d884c2c3bffdf9244cd44e7a53a2929a0f4b 100644 --- a/main.c +++ b/main.c @@ -4,16 +4,16 @@ * @brief Main file for the electric field exercice * @version 0.1 * @date 2021-07-12 - * + * * @copyright Copyright (c) 2021 - * + * */ +#include "charge/charge.h" +#include "gfx/gfx.h" +#include <math.h> #include <stdio.h> #include <stdlib.h> #include <time.h> -#include <math.h> -#include "gfx/gfx.h" -#include "charge/charge.h" #define WINDOW_WIDTH 500 #define WINDOW_HEIGHT 500 @@ -25,38 +25,37 @@ #define NB_POINTS 200 #define DELTA (1 / sqrt(pow(WINDOW_WIDTH, 2) + pow(WINDOW_HEIGHT, 2))) -int main(void) -{ +int main(void) { srand(time(NULL)); // GFX initialization - struct gfx_context_t *ctxt = gfx_create("draw", WINDOW_WIDTH, WINDOW_HEIGHT); - if (!ctxt) - { + struct gfx_context_t *ctxt = + gfx_create("draw", WINDOW_WIDTH, WINDOW_HEIGHT); + if (!ctxt) { fprintf(stderr, "Graphics initialization failed !\n"); return EXIT_FAILURE; } // Generate all the charges charge_t charges[NB_CHARGES]; - for (int i = 0; i < NB_CHARGES; i++) - { - charges[i] = charge_create(rand_one() / (rand_one() * 10 - 5), vec2_create(rand_one(), rand_one())); + for (int i = 0; i < NB_CHARGES; i++) { + charges[i] = charge_create(rand_one() / (rand_one() * 10 - 5), + vec2_create(rand_one(), rand_one())); } draw_charges(ctxt, charges, NB_CHARGES, x0, x1, y0, y1); // Generate all the points vec2 points[NB_POINTS]; generate_points(points, NB_POINTS); - for (int i = 0; i < NB_POINTS; i++) - { - coordinates_t c = position_to_coordinates(WINDOW_WIDTH, WINDOW_HEIGHT, x0, x1, y0, y1, points[i]); - draw_field_line(ctxt, charges, NB_CHARGES, DELTA, points[i], x0, x1, y0, y1); + for (int i = 0; i < NB_POINTS; i++) { + coordinates_t c = position_to_coordinates(WINDOW_WIDTH, WINDOW_HEIGHT, + x0, x1, y0, y1, points[i]); + draw_field_line(ctxt, charges, NB_CHARGES, DELTA, points[i], x0, x1, y0, + y1); } // GFX Draw loop - while (gfx_keypressed() != SDLK_ESCAPE) - { + while (gfx_keypressed() != SDLK_ESCAPE) { gfx_present(ctxt); } diff --git a/utilities/utils.c b/utilities/utils.c index c683fd200a3d027ec0322f5bf7c731d4ae020380..c157c55d563e980fba8e4cb12a87e3584cd4db32 100644 --- a/utilities/utils.c +++ b/utilities/utils.c @@ -1,23 +1,21 @@ +#include "utils.h" +#include "../vec2/vec2.h" #include <math.h> #include <stdlib.h> -#include "../vec2/vec2.h" -#include "utils.h" // Transform a position in the univers [x0,y0]x[x1,y1] to a screen position -coordinates_t position_to_coordinates(int width, int height, double x0, double x1, double y0, double y1, vec2 pos) -{ +coordinates_t position_to_coordinates(int width, int height, double x0, + double x1, double y0, double y1, + vec2 pos) { double dx = x1 - x0; double dy = y1 - y0; - return coordinates_create((int)round(height * (pos.y - y0) / dy), (int)round(width * (pos.x - x0) / dx)); + return coordinates_create((int)round(height * (pos.y - y0) / dy), + (int)round(width * (pos.x - x0) / dx)); } -double rand_one() -{ - return (double)rand() / (double)RAND_MAX; -} +double rand_one() { return (double)rand() / (double)RAND_MAX; } -charge_t charge_create(double q, vec2 pos) -{ +charge_t charge_create(double q, vec2 pos) { charge_t c = {.q = q, .pos = pos}; return c; } \ No newline at end of file diff --git a/utilities/utils.h b/utilities/utils.h index 8a9948d880dce02e3f440dc645e1131550151310..6f1f3dd221a99cfc0b756ce62493bd7d3187e324 100644 --- a/utilities/utils.h +++ b/utilities/utils.h @@ -1,18 +1,19 @@ #ifndef _UTILS_H_ #define _UTILS_H_ -#include <stdint.h> -#include "../vec2/vec2.h" #include "../draw/draw.h" +#include "../vec2/vec2.h" +#include <stdint.h> -typedef struct _charge_t -{ +typedef struct _charge_t { double q; vec2 pos; } charge_t; // Transform a position in the univers [x0,y0]x[x1,y1] to a screen position -coordinates_t position_to_coordinates(int width, int height, double x0, double x1, double y0, double y1, vec2 pos); +coordinates_t position_to_coordinates(int width, int height, double x0, + double x1, double y0, double y1, + vec2 pos); double rand_one();