Skip to content
Snippets Groups Projects
Commit 6f23a2dd authored by fardin.hossain's avatar fardin.hossain
Browse files

test first

parent 7667699c
Branches
No related tags found
No related merge requests found
Pipeline #6083 failed
#include "box_lib.h"
#include "vecteur_lib.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
box new_box(double x0, double x1, double y0, double y1) {
box la_boite;
la_boite.x0 = x0;
la_boite.x1 = x1;
la_boite.y0 = y0;
la_boite.y1 = y1;
return la_boite;
}
box *divide_in_four(box b) {
box* box_array = malloc(4 * sizeof(box));
double x0 = b.x0;
double x1 = b.x1;
double y0 = b.y0;
double y1 = b.y1;
box haut_gauche = new_box(x0, (x1 /2.0), (y1 / 2.0), y1);
box haut_droite = new_box((x1 /2.0), x1, (y1 / 2.0), y1);
box bas_gauche = new_box(x0, (x1 /2.0), y0, (y1 /2));
box bas_droite = new_box((x1 /2.0), x1, y0, (y1 /2));
box_array[0] = haut_gauche;
box_array[1] = haut_droite;
box_array[2] = bas_gauche;
box_array[3] = bas_droite;
return box_array;
}
bool is_inside(box b, vec v){
if ((v.x >= b.x0 && v.x <= b.x1) && ((v.y >= b.y0 && v.y <= b.y1))) {
return true;
}
else {
return false;
}
}
double compute_length(box b) {
double largeur = (b.x1 - b.x0);
double longueur = (b.y1 - b.y0);
if (longueur < largeur) {
return largeur;
}
else {
return longueur;
}
}
void print_box(box b) {
printf("x0 : {%f, %f} et x1 : {%f, %f}\n", b.x0, b.y0, b.x1, b.y1);
}
void tests_de_box() {
printf("hellox box\n");
box boite = new_box(0.0,20.0,0.0,30.0);
box* tab_de_boxes = divide_in_four(boite);
for (int i = 0; i < 4; i++) {
print_box(tab_de_boxes[i]);
}
vec* point = new_vec(15.0, 22.0);
bool dedans = is_inside(boite, *point);
printf("dedans ou pas ? (1 = vrai et 0 = faux) : %d\n", dedans);
printf("longue dist : %f\n", compute_length(boite));
}
\ No newline at end of file
#include "vecteur_lib.h"
#include <stdbool.h>
#if !defined(__BOX_LIB__H__)
#define __BOX_LIB__H__
typedef struct __box {
double x0, x1, y0, y1;
} box;
// Création d’une nouvelle box
box new_box(double x0, double x1, double y0, double y1);
// Division d’une box en quatre parties égales
box *divide_in_four(box b);
// Déterminer si une position est à l’intérieur de la box
bool is_inside(box b, vec v);
//Déterminer la taille maximale d’un des côtés de la box
double compute_length(box b);
// Affiche la box (utile pour le débogage)
void print_box(box b);
void tests_de_box();
#endif
\ No newline at end of file
all: box
box: box.o vecteur.o
gcc -o box box.o vecteur.o
vecteur.o : vecteur_lib.c vecteur_lib.h
gcc -o vecteur.o -c vecteur_lib.c -lm -W -Wall
box.o : box_lib.c
gcc -o box.o -c box_lib.c -W -Wall
star:
gcc -o star star_lib.c
bon
all: gal
.PHONY : all
gal : galaxy.o box.o vecteur.o star.o
gcc -o gal galaxy.o box.o vecteur.o star.o -lm
vecteur.o : vecteur_lib.c
gcc -o vecteur.o -c vecteur_lib.c -lm -W -Wall
box.o : box_lib.c
gcc -o box.o -c box_lib.c -W -Wall -lm
star.o : star_lib.c
gcc -o star.o -c star_lib.c -W -Wall -lm
galaxy.o : galaxy_lib.c
gcc -o galaxy.o galaxy_lib.c -W -Wall -lm
clean:
rm gal box.o vecteur.o star.o galaxy.o
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "galaxy_lib.h"
#include "star_lib.h"
#include "box_lib.h"
#include "box_lib.c" // cest nimporte quoi
#include "vecteur_lib.h"
#include <math.h>
#include <time.h>
#define G 6.67E-11
// Generer un nombre aléatoire (de type double)
double randFrom(double min, double max) {
double range = (max - min);
double div = RAND_MAX / range;
return min + (rand() / div);
}
galaxy *create_and_init_galaxy(int num_bodies, box b, double dt) {
star** stars = malloc(num_bodies * sizeof(star*));
// Initialization super star:
vec* zero_vec = new_vec(0.0, 0.0);
stars[0] = new_star_vel(
*zero_vec, // init pos
*zero_vec, // init velocity
*zero_vec, // init acceleration
(m_solar * pow(10.0, 6.0)), // mass
0.0 // init dt
);
for(int i = 1; i < num_bodies; i++) {
// Randomize a position
vec* r_i_prototype = new_vec(0.5 - randFrom(0.0, 1.0), 0.5 - randFrom(0.0, 1.0));
double alpha = R * (log10(1.0 - randFrom(0.0, 1.0))) / 1.8;
vec* r_i = mul_vec(alpha, r_i_prototype);
//print_vec(r_i_prototype);
//printf("alpha = %g\n", alpha);
//print_vec(r_i);
free(r_i_prototype);
// Find mass
double m_i = m_min + (randFrom(0.0, 10.0) * m_solar);
// Calculate initial speed
double phi = atan2(r_i->y, r_i->x);
vec* v_i_prototype = new_vec(-sin(phi), cos(phi));
vec* v_i = mul_vec(sqrt((G * (m_i + stars[0]->mass)) / norm(r_i)), v_i_prototype);
free(v_i_prototype);
// Add the star to the list
stars[i] = new_star_vel(*r_i, *v_i, *zero_vec, m_i, dt);
// Calculate acceleration
update_acceleration(stars[i], stars[0]);
}
galaxy* g = malloc(sizeof(galaxy));
g->num_bodies = num_bodies;
g->b = b;
g->stars = stars;
return g;
}
// m_i = m_min + m_i' m_i' = random(10.0) * m_solar
// r_i = R * ( (log(1 - random(1.0)) / 1.8) ) (0.5 - random(1.0)x, 0.5 - random(1.0 y))
// R = pow(10,18)
// m_0 = pow(10, 6) * m_solar, position = (0, 0), speed = (0, 0)
// Other stars have initial speed of:
// v_i = sqrt( (G(m_i + m_0)) / ||r_i|| ) * (-sin(phi), cos(phi))
// phi = atan2(r_iy / r_ix)
void reset_accelerations(galaxy* g) {
vec* zero_vec = new_vec(0.0, 0.0);
for(int i = 0; i < g->num_bodies; i++) {
if(g->stars[i] != NULL) {
g->stars[i]->acc = *zero_vec;
}
}
free(zero_vec);
}
void update_positions(galaxy *g, double dt) {
for(int i = 0; i < g->num_bodies; i++) {
update_position(g->stars[i], dt);
}
}
void free_galaxy(galaxy *g) { // NEED TO FREE THE VECTORS!!!
for(int i = 0; i < g->num_bodies; i++) {
//vec_free(&g->stars[i]->pos_t);
//vec_free(&g->stars[i]->pos_t_dt);
//vec_free(&g->stars[i]->acc);
free(g->stars[i]);
}
free(g->stars);
free(g);
}
void resize_galaxy(galaxy *g) {
// If a star pos vector is outside of the box-allowed boundaries, it needs to be:
// destroyed, freed, moved.
// Check whether a vector is out of the boundaries -> put the pointer the NULL
int q_2_rem = 0;
star** tmp = malloc(g->num_bodies * sizeof(star*));
for(int i = 0; i < g->num_bodies; i++) {
if(!is_inside(g->b, g->stars[i]->pos_t)) {
q_2_rem++;
} else {
tmp[i] = g->stars[i];
//print_star(g->stars[i]);
}
//print_star(g->stars[i]);
}
g->num_bodies = g->num_bodies - q_2_rem;
g->stars = realloc(g->stars, g->num_bodies * sizeof(star*));
if(g->num_bodies == 1) {
EXIT_SUCCESS;
}
for(int i = 0; i < g->num_bodies; i++) {
if(is_inside(g->b,
g->stars[i]->pos_t)) {
g->stars[i] = tmp[i];
//print_star(g->stars[i]);
}
}
free(tmp);
}
void test_de_galaxy(){
// Initialize seed for random random generation (seeder needs to be just outside the function)
galaxy* g = create_and_init_galaxy(5, new_box(-1.0, 2.0, -2.0, 1.0), 1.0);
for(int i = 1; i < 10; i++) {
update_acceleration(g->stars[1], g->stars[0]);
update_positions(g, (double)i * time_unit);
print_star(g->stars[1]);
}
}
\ No newline at end of file
#include "star_lib.h"
#include "box_lib.h"
#include "vecteur_lib.h"
#define time_unit 10000000000.0
#define R 1000000000000000000.0
#define m_solar 198892000000000000000.0
#define m_min 100000000000000000000.0
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
#if !defined(__GALAXY_LIB__H__)
#define __GALAXY_LIB__H__
typedef struct __galaxy {
int num_bodies;
star **stars;
box b;
} galaxy;
galaxy *create_and_init_galaxy(int num_bodies, box b, double dt);
void reset_accelerations(galaxy *g);
void update_positions(galaxy *g, double dt);
void free_galaxy(galaxy *g);
void resize_galaxy(galaxy *g);
void test_de_galaxy();
#endif
\ No newline at end of file
gfx.c 0 → 100644
/// @file gfx.c
/// @author Florent Gluck
/// @date November 6, 2016
/// Helper routines to render pixels in fullscreen graphic mode.
/// Uses the SDL2 library.
#include "gfx.h"
/// Create a fullscreen graphic window.
/// @param title Title of the window.
/// @param width Width of the window in pixels.
/// @param height Height of the window in pixels.
/// @return a pointer to the graphic context or NULL if it failed.
struct gfx_context_t* gfx_create(char *title, uint width, uint height) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) goto error;
SDL_Window *window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, width, height);
uint32_t *pixels = malloc(width*height*sizeof(uint32_t));
struct gfx_context_t *ctxt = malloc(sizeof(struct gfx_context_t));
if (!window || !renderer || !texture || !pixels || !ctxt) goto error;
ctxt->renderer = renderer;
ctxt->texture = texture;
ctxt->window = window;
ctxt->width = width;
ctxt->height = height;
ctxt->pixels = pixels;
SDL_ShowCursor(SDL_DISABLE);
gfx_clear(ctxt, COLOR_BLACK);
return ctxt;
error:
return NULL;
}
/// Draw a pixel in the specified graphic context.
/// @param ctxt Graphic context where the pixel is to be drawn.
/// @param x X coordinate of the pixel.
/// @param y Y coordinate of the pixel.
/// @param color Color of the pixel.
void gfx_putpixel(struct gfx_context_t *ctxt, int x, int y, uint32_t color) {
if (x < ctxt->width && y < ctxt->height)
ctxt->pixels[ctxt->width*y+x] = color;
}
/// Clear the specified graphic context.
/// @param ctxt Graphic context to clear.
/// @param color Color to use.
void gfx_clear(struct gfx_context_t *ctxt, uint32_t color) {
int n = ctxt->width*ctxt->height;
while (n)
ctxt->pixels[--n] = color;
}
/// Display the graphic context.
/// @param ctxt Graphic context to clear.
void gfx_present(struct gfx_context_t *ctxt) {
SDL_UpdateTexture(ctxt->texture, NULL, ctxt->pixels, ctxt->width*sizeof(uint32_t));
SDL_RenderCopy(ctxt->renderer, ctxt->texture, NULL, NULL);
SDL_RenderPresent(ctxt->renderer);
}
/// Destroy a graphic window.
/// @param ctxt Graphic context of the window to close.
void gfx_destroy(struct gfx_context_t *ctxt) {
SDL_ShowCursor(SDL_ENABLE);
SDL_DestroyTexture(ctxt->texture);
SDL_DestroyRenderer(ctxt->renderer);
SDL_DestroyWindow(ctxt->window);
free(ctxt->pixels);
ctxt->texture = NULL;
ctxt->renderer = NULL;
ctxt->window = NULL;
ctxt->pixels = NULL;
SDL_Quit();
free(ctxt);
}
/// If a key was pressed, returns its key code (non blocking call).
/// List of key codes: https://wiki.libsdl.org/SDL_Keycode
/// SDL_PumpEvents() must be called before.
/// @return 0 if escape was not pressed.
SDL_Keycode gfx_keypressed() {
const Uint8 *state = SDL_GetKeyboardState(NULL);
if (state && state[SDL_SCANCODE_ESCAPE]) {
return SDLK_ESCAPE;
}
return 0;
}
gfx.h 0 → 100644
#ifndef _GFX_H_
#define _GFX_H_
#include <stdint.h>
#include <SDL2/SDL.h>
#define MAKE_COLOR(r,g,b) ((uint32_t)b|((uint32_t)g<<8)|((uint32_t)r<<16))
#define COLOR_BLACK 0x00000000
#define COLOR_RED 0x00FF0000
#define COLOR_GREEN 0x0000FF00
#define COLOR_BLUE 0x000000FF
#define COLOR_WHITE 0x00FFFFFF
#define COLOR_YELLOW 0x00FFFF00
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned char uchar;
struct gfx_context_t {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
uint32_t *pixels;
int width;
int height;
};
extern void gfx_putpixel(struct gfx_context_t *ctxt, int x, int y, uint32_t color);
extern void gfx_clear(struct gfx_context_t *ctxt, uint32_t color);
extern struct gfx_context_t* gfx_create(char *text, uint width, uint height);
extern void gfx_destroy(struct gfx_context_t *ctxt);
extern void gfx_present(struct gfx_context_t *ctxt);
extern SDL_Keycode gfx_keypressed();
extern SDL_EventType poll_event();
#endif
main.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "galaxy_lib.h"
#include "star_lib.h"
#include "box_lib.h"
#include "vecteur_lib.h"
#include <math.h>
#include <time.h>
int main(int argc, char** argv) {
printf("boooms\n");
vec* hey_vec = new_vec(2.0,1.5);
box hey_box = new_box(-1.0, 1.0, -1.0, 1.0);
// star
vec* pos_t = new_vec(5.0, 5.0);
double dt = 2.0;
vec* vel = new_vec(1.5, 1.5);
double mass = 2.0;
vec* acc = new_vec(1.0,1.0);
star* etoiletest = new_star_vel(*pos_t, *vel, *acc, mass, dt);
// galaxy
star** tab_de_stars = malloc(2 * sizeof(star*));
tab_de_stars[0] = etoiletest;
tab_de_stars[1] = etoiletest;
galaxy* gal = create_and_init_galaxy(2, hey_box, 15.0);
free(hey_vec);
free(pos_t);
free(vel);
free(acc);
free(etoiletest);
// free(tab_de_stars[0]);
// free(tab_de_stars[1]);
free(tab_de_stars);
free_galaxy(gal);
test_de_galaxy();
printf("\nfin de testzer\n");
}
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "quad_tree.h"
#include "galaxy_lib.h"
#include "star_lib.h"
#include "box_lib.h"
#include "box_lib.c" // cest nimporte quoi
#include "vecteur_lib.h"
#include <math.h>
#include <time.h>
void insert_star(node* n, star* s) {
if( (is_inside(n->b, s->pos_t) == true) && (n)) {
if(!n->children[0] && !n->children[1] && !n->children[2] && !n->children[3]) {
if(n->is_empty) {
n->is_empty = 0;
n->s = s;
} else {
box* kids = divide_in_four(n->b);
for(int i = 0; i < 4; i++) {
n->children[i] = malloc(sizeof(node));
if(!n->children[i]) {
fprintf(stderr, "\nmalloc failed\n");
EXIT_FAILURE;
}
n->children[i]->b = kids[i];
n->children[i]->is_empty = 1;
n->children[i]->s = malloc(sizeof(star));
n->children[i]->super_s = malloc(sizeof(star));
//vec* zero = new_vec(0.0, 0.0);
//n->children[i]->super_s = new_star_vel(*zero, *zero, *zero, 0.0, 0.0);
for(int j = 0; j < 4; j++) {
n->children[i]->children[j] = NULL;
}
insert_star(n->children[i], n->s);
}
for(int i = 0; i < 4; i++) {
insert_star(n->children[i], s);
}
n->s = NULL;
n->is_empty = 1;
}
} else {
// Increment super star mass
n->super_s->mass += s->mass;
// Increment Center of mass coordinates
n->super_s->pos_t_dt = n->super_s->pos_t;
n->super_s->pos_t = *mul_vec((1.0 / n->super_s->mass), mul_vec(s->mass, add_vec(&n->super_s->pos_t, &s->pos_t)));
for(int i = 0; i < 4; i++) {
insert_star(n->children[i], s);
}
}
}
}
quad_tree *create_quad_tree_from_galaxy(const galaxy *const g) {
// Initialization quad-tree
box boundaries = g->b;
node* top = malloc(sizeof(node));
top->s = NULL;
top->super_s = g->stars[0];
top->is_empty = 1;
top->b = boundaries;
for(int j = 0; j < 4; j++) {
top->children[j] = NULL;
}
quad_tree* qt = malloc(sizeof(quad_tree));
qt->root = top;
// Populate the tree
for(int i = 1; i < g->num_bodies; i++) {
insert_star(qt->root, g->stars[i]);
}
return qt;
}
void free_node(node* n) {
if(!n) return;
for(int i = 0; i < 4; ++i) {
free_node(n->children[i]);
}
free(n);
}
void free_quad_tree(quad_tree* qt) {
free_node(qt->root);
free(qt);
}
void update_acceleration_from_node(const node *const n, star *s, double theta) {
//1. Si le nœud est une feuille non-vide et que l’étoile n’est pas dans le sousdomaine du nœud, on met à jour l’accélération entre l’étoile et l’étoile
//contenue dans le nœud.
if((!n->children[0]) && (!n->children[1]) && (!n->children[2]) && (!n->children[3])
&& (!n->is_empty) && !is_inside(n->b, s->pos_t)) {
update_acceleration(s, n->s);
} else if (compute_length(n->b) / (norm(sub_vec(&(n->super_s->pos_t), &(s->pos_t)))) < theta) {
update_acceleration(s, n->super_s);
} else {
for (int i = 0; i < 4; i++) {
if (n->children[i] != NULL) {
update_acceleration_from_node(n->children[i], s, theta);
}
}
}
//2. Sinon, si n est assez éloigné de s (si la taille du sous-domaine de n divisée
//par la distance entre la position de la super étoile de n et s est plus petite
//que theta) on met à jour l’accélération de s à l’aide de la super étoile de
//n.
//3. Sinon, pour tous les enfants de n, on rappelle
//update_acceleration_from_node(n, s, theta);
}
void update_accelerations_of_all_stars(const node *const n, galaxy *g, double theta) {
for(int i = 0; i < g->num_bodies; i++) {
update_acceleration_from_node(n, g->stars[i], theta);
}
}
void test_quad_tree_lib() {
box boundaries = new_box(-15.0, 15.0, -15.0, 15.0);
star** my_stars = malloc(30 * sizeof(star*));
for(int i = 0; i < 30; i++) {
my_stars[i] = malloc(sizeof(star));
my_stars[i]->pos_t = *new_vec(0.5 + (double)i, 0.5 + (double)i);
my_stars[i]->pos_t_dt = *new_vec(0.5 + (double)i, 0.5 + (double)i);
my_stars[i]->mass = 20.0;
my_stars[i]->acc = *new_vec(0.5 + (double)i, 0.5 + (double)i);
}
// Initialization node
node* top = malloc(sizeof(node));
top->s = NULL;
top->super_s = my_stars[0];
top->is_empty = 1;
top->b = boundaries;
for(int j = 0; j < 4; j++) {
top->children[j] = NULL;
}
// Stars to be inserted
for(int i = 0; i < 30; i++) {
printf("\nATTEMPT TO INSERT --------------------------------------------\n");
insert_star(top, my_stars[i]);
// print_star(my_stars[i]);
}
galaxy* g = create_and_init_galaxy(10, new_box(-100000000000000000000000.0, 100000000000000000000000.0, -100000000000000000000000.0, 100000000000000000000000.0), 1.0 * time_unit);
quad_tree* qt = create_quad_tree_from_galaxy(g);
update_accelerations_of_all_stars(qt->root, g, 0.1);
printf("\nEND\n");
}
#include "star_lib.h"
#include "box_lib.h"
#include "vecteur_lib.h"
#include "galaxy_lib.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
#if !defined(__QUAD_TREE__H__)
#define __QUAD_TREE__H__
typedef struct __quad_tree {
node *root;
} quad_tree;
typedef struct __node {
struct __node *children[4];
box b;
star *s;
star *super_s;
bool is_empty;
} node;
// Création du quad_tree:
quad_tree *create_quad_tree_from_galaxy(const galaxy *const g);
void insert_star(node *n, star *s);
// Libération de la mémoire du quad_tree
void free_quad_tree(quad_tree *t);
// Mise à jour de l’accélération d’une étoile:
void update_acceleration_from_node(const node *const n, star *s, double theta);
// Mise à jour de l’accélération de toutes les étoiles:
void update_accelerations_of_all_stars(const node *const n, galaxy *g, double theta);
#endif
\ No newline at end of file
#include "star_lib.h"
#include "box_lib.h"
#include "vecteur_lib.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
star *new_star_vel(vec pos, vec vel, vec acc, double mass, double dt) {
star* etoile = malloc(sizeof(star));
etoile->pos_t = pos;
vec* vitesse = new_vec(vel.x, vel.y);
vec* position = new_vec(pos.x, pos.y);
vec* position_t_dt = sub_vec(position, mul_vec(dt, vitesse));
etoile->pos_t_dt = *sub_vec(position, mul_vec(dt, vitesse)); //*new_vec(position_t_dt->x, position_t_dt->y);
etoile->acc = acc;
etoile->mass = mass;
return etoile;
}
void reset_acceleration(star *s) {
s->acc = *new_vec(0.0, 0.0);
}
void update_acceleration(star *s, const star *const s2) {
//qudn on met a jour cest += pas =
//acc += (Fij /mi)
double G = 6.67E-11;
double m_s = s->mass;
double m_s2 = s2->mass;
vec* r_s2_moins_r_s = sub_vec(&(s2->pos_t), &(s->pos_t)); //
double mod_r_s_s2 = norm(sub_vec(&(s->pos_t), &(s2->pos_t))); //
double epsilon = 3E4;
double denominateur = pow((mod_r_s_s2 + epsilon), 3);
printf("test 2 ^ 3 = %f (huit) \n" , pow(2,3));
vec* numerateur = mul_vec((G * m_s * m_s2), r_s2_moins_r_s);
vec* force = mul_vec((1.0/denominateur), numerateur);
s->acc = *add_vec(&(s->acc), force); //
}
void update_position(star *s, double dt) {
vec* the_pos_t = &(s->pos_t);
vec* the_pos_dt = &(s->pos_t_dt);
vec* dt_foix_acc = mul_vec(dt*dt, &(s->acc));
s->pos_t = *(add_vec(( sub_vec(mul_vec(2.0, the_pos_t), the_pos_dt)), dt_foix_acc));
}
void print_star(const star *const s) {
printf("Star { pos_t : {%f, %f}, post_t_dt : {%f, %f}, acc : {%f, %f}, mass : %f}\n", s->pos_t.x, s->pos_t.y, s->pos_t_dt.x, s->pos_t_dt.y, s->acc.x, s->acc.y, s->mass);
}
void tests_de_stars(){
printf("hey salut a tous \n");
vec* vecteur_test = new_vec(0.0, 0.0);
vec* pos_t = new_vec(5.0, 5.0);
double dt = 2.0;
vec* vel = new_vec(1.5, 1.5);
double mass = 2.0;
vec* acc = new_vec(1.0,1.0);
star* etoiletest = new_star_vel(*pos_t, *vel, *acc, mass, dt);
printf("pos_t_dt x : %f et y : %f\n", etoiletest->pos_t_dt.x, etoiletest->pos_t_dt.y);
printf("\n\n\n");
print_star(etoiletest);
printf("\n\n\n");
printf("test & et *\n");
vec* test_pointeur = new_vec(1.4,2.5);
vec le_test_pointeur = *test_pointeur;
printf("x : %f et y : %f\n", test_pointeur->x, test_pointeur->y);
printf("x : %f et y : %f\n", le_test_pointeur.x, le_test_pointeur.y);
vec addr_test_pointeur;
addr_test_pointeur.x = 5.2;
addr_test_pointeur.y = 3.8;
vec* adr_le_test_pointeur = &addr_test_pointeur;
printf("x : %f et y : %f\n", addr_test_pointeur.x, addr_test_pointeur.y);
printf("x : %f et y : %f\n", adr_le_test_pointeur->x, adr_le_test_pointeur->y);
printf("\ntest box is inside\n");
}
\ No newline at end of file
#include "vecteur_lib.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
#if !defined(__STAR_LIB__H__)
#define __STAR_LIB__H__
typedef struct __star {
vec pos_t, pos_t_dt, acc;
double mass;
} star;
// Création d’une nouvelle étoile à la position pos_t, vitesse vel, accélération acc, masse mass et dt la discrétisation temporelle:
star *new_star_vel(vec pos, vec vel, vec acc, double mass, double dt);
// On initialisera pos_t_dt à l’aide de la relation (en pseudo-code) pos_t_dt = pos_t - dt * vel
// Remise à zéro de l’accélération d’une étoile:
void reset_acceleration(star *s);
// Mise à jour de l’accélération d’une étoile, s , à cause de l’attraction gravitationnelle d’une autre étoile, s2 :
void update_acceleration(star *s, const star *const s2);
// Mise à jour de la position d’une étoile, avec dt la discrétisation temporelle:
void update_position(star *s, double dt);
// En pseudo-code cette mise à jour est de la forme: pos_t = 2*pos_t - pos_t_dt + acc * dt * dt
// Affichage des champs d’une étoile pour aider au débogage:
void print_star(const star *const s);
#endif
\ No newline at end of file
#include "vecteur_lib.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
vec *new_vec(double x, double y) {
vec* le_vecteur = malloc(sizeof(vec));
le_vecteur->x = x;
le_vecteur->y = y;
return le_vecteur;
}
vec *add_vec(const vec *const v1, const vec *const v2) {
double le_x = (v1->x + v2->x);
double le_y = (v1->y + v2->y);
vec* result_vec = new_vec(le_x, le_y);
return result_vec;
}
vec *sub_vec(const vec *const v1, const vec *const v2) {
double le_x = (v1->x - v2->x);
double le_y = (v1->y - v2->y);
vec* result_vec = new_vec(le_x, le_y);
return result_vec;
}
vec *mul_vec(double alpha, const vec *const v2) {
double le_x = (v2->x * alpha);
double le_y = (v2->y * alpha);
vec* result_vec = new_vec(le_x, le_y);
return result_vec;
}
double norm(const vec *const v1) {
return sqrt((v1->x * v1->x) + (v1->y * v1->y));
}
double distance(const vec *const v1, const vec *const v2) {
return sqrt(( (v2->x - v1->x) * (v2->x - v1->x) ) + ((v2->y - v1->y) * (v2->y - v1->y)));
}
void print_vec(const vec *const v) {
printf("{%f, %f}", v->x, v->y);
}
void tests_vecteurs() {
printf("hey salut a tous \n");
vec* the_vec = new_vec(3.0,2.0);
printf("x = %f et y = %f \n", the_vec->x, the_vec-> y);
printf("test add \n");
vec* v1 = new_vec(3.0,2.0);
vec* v2 = new_vec(5.0,-4.5);
vec* result_add = add_vec(v1, v2);
printf("x = %f et y = %f \n", result_add->x, result_add-> y);
printf("test sub \n");
vec* v3 = new_vec(3.0,2.0);
vec* v4 = new_vec(5.0,-4.5);
vec* result_sub = sub_vec(v3, v4);
printf("x = %f et y = %f \n", result_sub->x, result_sub-> y);
printf("test mul \n");
vec* v5 = new_vec(3.0,2.0);
double multiplicateur = 5.34;
vec* result_mul = mul_vec(multiplicateur, v5);
printf("x = %f et y = %f \n", result_mul->x, result_mul-> y);
printf("test norme \n");
vec* v6 = new_vec(4.0,3.0);
double norme = norm(v6);
printf("x = %lf \n", norme);
print_vec(v6);
}
#if !defined(__VECTEUR_LIB__H__)
#define __VECTEUR_LIB__H__
typedef struct __vec {
double x, y;
} vec;
// Creer et initialiser un vecteur
vec *new_vec(double x, double y);
// Additionner deux vecteurs
vec *add_vec(const vec *const v1, const vec *const v2);
// Soustraire deux vecteurs
vec *sub_vec(const vec *const v1, const vec *const v2);
// Multiplier un vecteur par un scalaire
vec *mul_vec(double alpha, const vec *const v2);
// Calculer la norme d’un vecteur
double norm(const vec *const v1);
// Calculer la distance entre deux vecteurs
double distance(const vec *const v1, const vec *const v2);
// Afficher un vecteur (ça vous aidera pour le débogage)
void print_vec(const vec *const v);
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment