원점을 지나는 2차방정식의 그래프를 그리는 프로그램인데요.
x값을 대입해서 y값을 구하는 방식이거든요.
x값이야 입력값이니깐 for문에서 제한해주면되었는데요.
float형으로 선언된 y값이 if에서 참조가 안되거든요.
아래 부분에서
float y,x; //xy함수에 값넣기
for(x=-10;x<10;x=x+0.01){
y=x*(co2*x+co1);
putpixel((int)(x*30+320),(int)(y*30*(-1)+239),LIGHTRED);
}
여기서 y값을 if(-4<y<4)이런식으로 제한을 둬서 그래프를 |4|넘어 가는건 자르고싶은데
y값이 참조가 안되네요.
뭐가 문제인지 좀 가르쳐주세요.
=======================================================
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<dos.h>
#define m 30
void main(){
int co1,co2;
printf("co2,co1?");
scanf("%d,%d",&co2,&co1);
//---------------
char *str; //x축 글자 출력용 변수
int gdriver=DETECT, gmode,errorcode;
int i,midx,midy; //중앙값 저장 변수
int temp;
initgraph(&gdriver,&gmode,""); //그래픽 모드로 변환
cleardevice(); //화면 클리어
setbkcolor(WHITE); //바탕화면 흰색
midx=getmaxx()/2+0.5; //x축 화면중앙
midy=getmaxy()/2+0.5; //y축 화면중앙
//축그리기
setcolor(GREEN); //그리기색 녹색
rectangle(0,0,getmaxx(),getmaxy()); //좌측 상단에서 하단까지 사각형 박스
moveto(0,midy); //x=0,y=mid 로 좌표를 옮김
lineto(getmaxx(),midy); //x=max,y=mid로 줄긋기
moveto(midx,0); //y축에 대해서도 반복실행
lineto(midx,getmaxy()); //
moveto(5,2);
outtext("2001440174 Jeong Sang Hyeok");
moveto(5,12);
sprintf(str,"x:%d, y:%d ",getmaxx(),getmaxy());
outtext(str);
moveto(5,22);
sprintf(str,"f(x) = x(%dx+%d) ",co2,co1);
outtext(str);
moveto(getmaxx()-10,midy+20);//화면 우측끝 중앙에 X를 씀(x축 표시)
outtext("X");
moveto(midx+10,10);//화면 상단 중앙에 Y씀(y축 표시)
outtext("Y");
//x축 눈금,숫자 써넣기
for(temp=midx-m*10,i=-10;temp<=midx+m*10 ;temp+=30,i++){ //x축의 의 좌측부터 30px 증가
moveto(midx-m*i,midy-5); //눈금 그리기
lineto(midx-m*i,midy+5);
sprintf(str,"%d",-i); //str에 i값을 문자열로 저장
outtextxy(midx-m*i,midy+10,str); //해당 좌표에 문자 출력
}
for(temp=midy-m*10,i=-7;temp<=midy+m*10 ;temp+=30,i++){ //y축의 의 좌측부터 30px 증가
moveto(midx-5,midy-m*i); //눈금 그리기
lineto(midx+5,midy-m*i);
sprintf(str,"%d",i); //str에 i값을 문자열로 저장
if(i!=0){
outtextxy(midx-20,midy-m*i,str); //해당 좌표에 문자 출력
}
}
float y,x; //xy함수에 값넣기
for(x=-10;x<10;x=x+0.01){
y=x*(co2*x+co1);
putpixel((int)(x*30+320),(int)(y*30*(-1)+239),LIGHTRED);
}
getch();
closegraph();
}
=================================
|