2017-04-04 9 views
-1

현재 사각형 코드 작업에 대한 내 요점을 얻으려고합니다. 슬프게도 부동 소수점 예외가 발생하지만 이유는 알 수 없습니다. 나는 그것이 가능한 제로 부문 때문에 그것이 처음으로 생각했지만 나는 그것을 배제했다. 또한 매번 int로 캐스팅 된 것처럼 보이므로 부동 소수점이 없어야합니다.C - 부동 소수점 예외

#include <stdio.h> 
#include <stdlib.h> 
#include <ncurses.h> 
#include <time.h> 

int point_on_line(int x, int y, int x1, int y1, int x2, int y2) { 
    int eq1 = (y2 - y1)/(x2 - x1); 
    int eq2 = eq1 * (x - x1); 
    int eq3 = y - y1 - eq2; 
    return eq3; 
} 

int point_in_rectangle(int x, int y, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { 
    int l1 = point_on_line(x, y, x1, y1, x2, y2); 
    int l2 = point_on_line(x, y, x2, y2, x3, y3); 
    int l3 = point_on_line(x, y, x3, y3, x4, y4); 
    int l4 = point_on_line(x, y, x4, y4, x1, y1); 
    if ((l1 <= 0) && (l2 <= 0) && (l3 <= 0) && (l4 <= 0)) { 
     return 1; 
    } 
    return 0; 
} 
int main() { 
initscr(); 
noecho(); 
nodelay(stdscr, TRUE); 

int x_max, y_max; 

getmaxyx(stdscr, y_max, x_max); 
srand(time(NULL)); 

start_color(); 
init_pair(0, COLOR_WHITE, COLOR_BLACK); 
init_pair(1, COLOR_RED, COLOR_BLACK); 
init_pair(2, COLOR_GREEN, COLOR_BLACK); 
init_pair(3, COLOR_BLUE, COLOR_BLACK); 
init_pair(4, COLOR_YELLOW, COLOR_BLACK); 
init_pair(5, COLOR_MAGENTA, COLOR_BLACK); 
init_pair(6, COLOR_CYAN, COLOR_BLACK); 

int colors[x_max][y_max]; 
for (int x = 0; x < x_max; x++) { 
    for (int y = 0; y < y_max; y++) { 
     int col = 0; 
     if (point_in_rectangle(x, y, 5, 5, 10, 5, 10, 10, 5, 10) == 1) { 
      col = 1; 
     } 
     colors[x][y] = col; 
    } 
} 

char input = '0'; 
while(1) { 
    char ch = getch(); 
    if (ch != ERR) { 
     input = ch; 
    } 

    for (int x = 0; x < x_max; x++){ 
     for (int y = 0; y < y_max; y++) { 
      int col = colors[x][y]; 
      attron(COLOR_PAIR(col)); 
      mvaddch(y, x, rand() % 200); 
      attroff(COLOR_PAIR(col)); 
     } 
    } 
    refresh(); 
} 

endwin(); 

return EXIT_SUCCESS; 
} 

컴파일하고 다음과 같은 오류 메시지가 제공하는 프로그램을 실행 한 후 :

Floating point exception (core dumped) 
+2

게시물에 정확한 오류 메시지를 포함하십시오. – ForceBru

+0

죄송합니다. 게시물을 편집했습니다. – nn3112337

+2

어떻게 0으로 나누는 것을 배제 했습니까? 그것이 바로 문제입니다. –

답변

4

는 기능 point_on_line에서 X1과 X2의 값에 가까이 봐. x1과 x2는 모두 5이고 x2 - x1은 0입니다. 기본적으로 부동 소수점 예외를주는 ZERO로 나누고 있습니다.

+0

C는 _methods_를 지원하지 않습니다. 그리고 아무도 없습니다. – Olaf

+0

잘못된 용어에 대해 유감입니다. 나는'기능'을 의미했다. – alDiablo