jaunes
This commit is contained in:
15
HolidayTrip/Fundamentals/radar/Makefile
Normal file
15
HolidayTrip/Fundamentals/radar/Makefile
Normal file
@@ -0,0 +1,15 @@
|
||||
CC=gcc
|
||||
SRC = radar.c main.c
|
||||
CFLAGS= -Wall -Wextra -Werror
|
||||
LDFLAGS= -fsanitize=address -g -lm
|
||||
|
||||
# Computes the basic main for tests
|
||||
main: ${SRC}
|
||||
gcc $(CFLAGS) $^ -o main -lm
|
||||
|
||||
#Check for memory errors/leaks and to use gdb
|
||||
debug: ${SRC}
|
||||
gcc $(CFLAGS) $^ -o debug $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
$(RM) main debug
|
||||
BIN
HolidayTrip/Fundamentals/radar/main
Executable file
BIN
HolidayTrip/Fundamentals/radar/main
Executable file
Binary file not shown.
43
HolidayTrip/Fundamentals/radar/main.c
Normal file
43
HolidayTrip/Fundamentals/radar/main.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "radar.h"
|
||||
|
||||
int main(){
|
||||
// compute_distance
|
||||
struct point p1 = { 0.0, 0.0 };
|
||||
struct point p2 = { 3.0, 4.0 };
|
||||
|
||||
double dist = compute_distance(p1, p2);
|
||||
printf("Distance: %.2f\n", dist);
|
||||
|
||||
// is_in_area
|
||||
struct point center = { 5.0, 5.0 };
|
||||
double radius = 3.0;
|
||||
|
||||
struct point inside_point = { 7.0, 7.0 };
|
||||
struct point outside_point = { 9.0, 9.0 };
|
||||
|
||||
int inside = is_in_area(center, radius, inside_point);
|
||||
int outside = is_in_area(center, radius, outside_point);
|
||||
|
||||
printf("Inside: %d\n", inside);
|
||||
printf("Outside: %d\n", outside);
|
||||
|
||||
// detect_points
|
||||
center.x = 10.0;
|
||||
center.y = 10.0;
|
||||
double range = 5.0;
|
||||
|
||||
struct point points[] = {
|
||||
{ 11.0, 11.0 },
|
||||
{ 10.0, 10.0 },
|
||||
{ 20.0, 20.0 },
|
||||
{ 14.0, 13.0 }
|
||||
};
|
||||
|
||||
size_t size = sizeof(points) / sizeof(struct point);
|
||||
|
||||
size_t found = detect_points(center, range, points, size);
|
||||
printf("Total found: %zu\n", found);
|
||||
|
||||
}
|
||||
31
HolidayTrip/Fundamentals/radar/radar.c
Normal file
31
HolidayTrip/Fundamentals/radar/radar.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "radar.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
double compute_distance(struct point a, struct point b){
|
||||
|
||||
return sqrt( pow((b.x - a.x),2) + pow((b.y - a.y),2) );
|
||||
}
|
||||
|
||||
int is_in_area(struct point center, double radius, struct point point){
|
||||
|
||||
int rslt = compute_distance(center, point);
|
||||
|
||||
if(rslt > radius) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t detect_points(struct point center, double range, struct point *interesting_points, size_t nb_points){
|
||||
|
||||
size_t found = 0;
|
||||
for(size_t i = 0; i < nb_points; i++){
|
||||
|
||||
if(is_in_area(center, range,interesting_points[i]) == 1){
|
||||
printf("{ %f, %f }\n",interesting_points[i].x, interesting_points[i].y);
|
||||
found ++;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
16
HolidayTrip/Fundamentals/radar/radar.h
Normal file
16
HolidayTrip/Fundamentals/radar/radar.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef RADAR_H
|
||||
#define RADAR_H
|
||||
#include <stdio.h>
|
||||
|
||||
struct point
|
||||
{
|
||||
double x;
|
||||
double y;
|
||||
};
|
||||
|
||||
double compute_distance(struct point a, struct point b);
|
||||
int is_in_area(struct point center, double radius, struct point point);
|
||||
size_t detect_points(struct point center, double range, struct point* interesting_points, size_t nb_points);
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user