Created
October 6, 2012 14:06
-
-
Save mccraveiro/3845013 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#define MAX 1000 | |
typedef struct { | |
int x, y; | |
} Point; | |
typedef struct { | |
Point a, b; | |
} Segment; | |
double min(double a, double b){ | |
return a > b ? b : a; | |
} | |
double distanceToSegment(Point pt, Segment s){ | |
Point p1 = s.a; | |
Point p2 = s.b; | |
double dx = p2.x - p1.x; | |
double dy = p2.y - p1.y; | |
// Calcular a distancia minima do ponto à reta | |
double t = ((pt.x - p1.x) * dx + (pt.y - p1.y) * dy) / (dx * dx + dy * dy); | |
// Verifica se o ponto pertence ao segmento | |
if (t < 0) { | |
// fora do segmento, p1 é o ponto mais próximo | |
dx = pt.x - p1.x; | |
dy = pt.y - p1.y; | |
} else if (t > 1) { | |
// fora do segmento, p2 é o ponto mais próximo | |
dx = pt.x - p2.x; | |
dy = pt.y - p2.y; | |
} else { | |
// ponto pertence ao segmento | |
dx = pt.x - (p1.x + t * dx); | |
dy = pt.y - (p1.y + t * dy); | |
} | |
return sqrt(dx * dx + dy * dy); | |
} | |
int main(){ | |
int n, l, h, i; | |
double resultado; | |
Segment aletas[MAX]; | |
while (scanf("%d", &n) != EOF) { | |
resultado = 999999999; | |
scanf("%d %d", &l, &h); | |
for (i = 0; i < n; i++) { | |
scanf("%d %d %d", &aletas[i].a.y, &aletas[i].b.x, &aletas[i].b.y); | |
if (i % 2 == 0) { | |
aletas[i].a.x = 0; | |
resultado = min(resultado, l - aletas[i].b.x); | |
} else { | |
aletas[i].a.x = l; | |
resultado = min(resultado, aletas[i].b.x); | |
} | |
} | |
for (i = 0; i < n-1; i++) { | |
resultado = min(resultado, distanceToSegment(aletas[i].b, aletas[i+1])); | |
} | |
printf("%.2lf\n", resultado); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment