Задача 6. График

Нарисовать на графическом экране график функции y=sin (2x) + + ^-cos2(x/2), 1<x<10.

{ Решение задачи 6 на языке PASCAL } {$X+}

USES Graph,Crt; CONST x1=1.0; x2=10.0;

FUNCTION f(x:Real):Real; BEGIN f:=Sqr(Sin(2*x))+Sqrt(x*Sqr(Cos(x/2))); END; CONST X0=100; Y0=400; Lx=400; Ly=300; Blank=40; TicX=9; TicY=10; TicSize=5;

ScreenColor=9; FieldColor=7; AxisColor=15; LineColor=14; TextColor=9;

TextStyle=1; TextSize=4; N=100; h=(x2-x1)/N; VAR GraphDriver,GraphMode,i        : Integer;

x,y,Ymin,Ymax,Mx,My : Real;

s : String;

BEGIN Ymin:=+1.7E38; Ymax:=-1.7E38;

FOR i:=0 TO N DO BEGIN y:=f(x1+i*h);

IF y<Ymin THEN Ymin:=y; IF y>Ymax THEN Ymax:=y;

END;

Mx:=Lx/(x2-x1); My:=Ly/(Ymax-Ymin); GraphDriver:=Detect; InitGraph(GraphDriver,GraphMode,''); SetFillStyle(8,ScreenColor); Bar(0,0,GetMaxX,GetMaxY); SetFillStyle(SolidFill,FieldColor); Bar(Blank,Blank,GetMaxX-Blank,GetMaxY-Blank); SetColor(AxisColor); SetTextStyle(TextStyle,0,TextSize); MoveTo(X0,Y0-Ly-TicSize);

SetTextJustify(CenterText,BottomText); OutText('Y'); MoveTo(X0,Y0-Ly); LineRel(0,Ly); LineRel(Lx,0); SetTextJustify(LeftText,BottomText); MoveRel(TicSize,0); OutText('X'); SetTextStyle(TextStyle,0,1); SetTextJustify(CenterText,TopText);

FOR i:=1 TO TicX DO BEGIN

x:=x1+i*(x2-x1)/TicX; Str(x:0:1,s); MoveTo(X0+Round((x-x1)*Mx),Y0); LineRel(0,TicSize); OutText(s);

END;

SetTextJustify(RightText,CenterText); FOR i:=1 TO TicY DO BEGIN

y:=Ymin+i*(Ymax-Ymin)/TicY; Str(y:0:2,s);

MoveTo(X0,Y0-Round((y-Ymin)*My));

LineRel(-TicSize,0); OutText(s);

END;

SetColor(TextColor); SetTextStyle(TextStyle,0,TextSize); SetTextJustify(CenterText,BottomText);

OutTextXY(X0+Lx DIV 2,Y0-Ly-TextHeight("),'y=sin2(2x)+Vx- cos2(x/2)'); SetColor(LineColor); SetLineStyle(0,0,3); SetViewPort(X0,Y0,GetMaxX,GetMaxY,FALSE); MoveTo(0,-Round((f(x1)-Ymin)*My)); FOR i:=1 TO N DO BEGIN x:=x1+i*h;

LineTo(Round((x-x1)*Mx),-Round((f(x)-Ymin)*My));

END;

ReadKey; CloseGraph;

END.

/* Решение задачи 6 на языке C */

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

#define x1 1.0

#define x2 10.0

#define F(x) (pow(sin(2*(x)),2)+sqrt((x)*pow(cos((x)/2),2))) #define ROUND(x) (floor((x)+.5)) void main(void){

int X0=100,Y0=400,Lx=400,Ly=300,Blank=40,TicX=9,TicY=10,TicSize=5,

ScreenColor=9,FieldColor=7,AxisColor=15,LineColor=14,TextColor=9, TextStyle=1,TextSize=4,N=100,GraphDriver,GraphMode,i; float h=(x2-x1)/N,x,y,Ymin=+1.7E38,Ymax=-1.7E38,Mx,My; char*s=(char*)malloc(10);

for(i=0;i<=N;i++){y=F(x1+i*h);Ymin=(y<Ymin)?y:Ymin;

Ymax=(y>Ymax)?y:Ymax;}

Mx=Lx/(x2-x1); My=Ly/(Ymax-Ymin); GraphDriver=DETECT;

initgraph(&GraphDriver,&GraphMode,""); setfillstyle(8,ScreenColor);

bar(0,0,getmaxx(),getmaxy()); setfillstyle(SOLID_FILL,FieldColor); bar(Blank,Blank,getmaxx()-Blank,getmaxy()-Blank); setcolor(AxisColor); settextstyle(TextStyle,0,TextSize); moveto(X0,Y0-Ly-TicSize); settextjustify(CENTER_TEXT,BOTTOM_TEXT); outtext("Y"); moveto(X0,Y0-Ly); linerel(0,Ly); linerel(Lx,0); settextjustify(LEFT_TEXT,BOTTOM_TEXT); moverel(TicSize,0); outtext("X");

settextstyle(TextStyle,0,1); settextjustify(CENTER_TEXT,TOP_TEXT); for(i=1;i<=TicX;i++){x=x1+i*(x2-x1)/TicX;sprintf(s,"%.1f^',x); moveto(X0+ROUND((x-x1)*Mx),Y0);linerel(0,TicSize);outtext(s);} settextjustify(RIGHT_TEXT,CENTER_TEXT);

for(i=1;i<=TicY;i++){y=Ymin+i*(Ymax-Ymin)/TicY;sprintf(s,"%.2f^',y); moveto(X0,Y0-ROUND((y-Ymin)*My));linerel(-TicSize,0);outtext(s);} setcolor(TextColor); settextstyle(TextStyle,0,TextSize); settextjustify(CENTER_TEXT,BOTTOM_TEXT); outtextxy(X0+Lx/2,Y0-Ly-textheight(""),"y=sin2(2x)+Vx- cos2(x/2)"); setcolor(LineColor); setlinestyle(0,0,3); setviewport(X0,Y0,getmaxx(),getmaxy(),0); moveto(0,-ROUND((F(x1)-Ymin)*My)); for(i=1;i<=N;i++) { x=x1+i*h;

lineto(ROUND((x-x1)*Mx),-ROUND((F(x)-Ymin)*My));} getch(); closegraph();}

C Решение задачи 6 на языке FORTRAN

$INCLUDE:'FGRAPH.FI'

$INCLUDE:'FGRAPH.FD'

PARAMETER(X1=1.,X2=10.,N=100,X0=100,Lx=400,Y0=400,Ly=300,

* Blank=40,TicX=9,TicY=10,TicSize=5,ScreenColor=9,

* FieldColor=7,AxisColor=15,LineColor=14,TextColor=9,

* LineStyle=#FFFF)

RECORD /XYCoord/ xy

CHARACTER*4 s,Font/'n4,/,FontXY/,n2,/,Path*80/,*.fon'/

RECORD /FontInfo/ FInf

RECORD/videoconfig/video REAL mx,my

INTEGER*1 Mask0(8)/#FF,#FF,#FF,#FF,#FF,#FF,#FF,#FF/,

* Mask(8) /#0F,#0F,#0F,#0F,#0F,#0F,#0F,#0F/ f(x)=SIN(2*x)**2+SQRT(x*Cos(x/2)**2) h=(X2-X1)/n

Ymin=+1.7e38

Ymax=-1.7e38

DO 1 i=0,n

x=X1+i*h

y=f(x)

IF(Ymin.GT.y) Ymin=y IF(Ymax.LT.y) Ymax=y

I CONTINUE mx=Lx/(X2-X1) my=Ly/(Ymax-Ymin)

idummy=SetVideoMode($MAXRESMODE) IF(idummy.EQ.O) STOP 'Graphics error' CALL GetVideoConfig(video) idummy=SetColor(ScreenColor) CALL SetFillMask(Mask) idummy=Rectangle($GFILLINTERIOR,0,0,

* video.NumXPixels-1,video.NumYPixels-1)

CALL SetFillMask(Mask0)

idummy=SetColor(FieldColor) idummy=Rectangle($GFILLINTERIOR,Blank,Blank,

* video.NumXPixels-1-Blank,video.NumYPixels-1-Blank) NumFonts=RegisterFonts(Path)

idummy=SetFont(Font) idummy=GetFontInfo(FInf) idummy=SetColor(AxisColor) CALL MoveTo(X0,Y0-LY,xy) idummy=LineTo(X0,Y0) idummy=LineTo(X0+LX,Y0) DO 11 i=1,TicX x=X1+i*(X2-X1)/TicX

CALL MoveTo(X0+NINT(mx*(x-X1)),Y0,xy) idummy=LineTo(X0+NINT(mx*(x-X1)),Y0+TicSize) WRITE(s,1000) x

CALL MoveTo(X0+NINT(mx*(x-X1))-GetGTextExtent(s)/2,Y0+TicSize,

* xy)

CALL OutGText(s)

II CONTINUE

DO 12 i=1,TicY

y=Ymin+i*(Ymax-Ymin)/TicY

CALL MoveTo(X0,Y0-NINT(my*(y-Ymin)),xy)

idummy=LineTo(X0-TicSize,Y0-NINT(my*(y-Ymin)))

WRITE(s,1000) y

CALL MoveTo(X0-GetGTextExtent(s)-TicSize,

* Y0-NINT(my*(y-Ymin))-FInf.PixHeight/2,xy) CALL OutGText(s)

12 CONTINUE

1000 FORMAT(F4.1)

idummy=SetFont(FontXY)

idummy=GetFontInfo(FInf)

CALL MoveTo(X0,Y0-Ly-FInf.PixHeight,xy)

CALL OutGText('Y')

CALL MoveTo(X0+Lx,Y0-FInf.PixHeight,xy) CALL OutGText('X')

CALL SetLineStyle(LineStyle)

idummy=SetColor(LineColor)

CALL MoveTo(X0,Y0-NINT(my*(f(X1)-Ymin)),xy) DO 2 i=1,n

x=X1+i*h

y=f(x)

idummy=LineTo(X0+NINT(mx*(x-X1)),Y0-NINT(my*(y-Ymin)))

2 CONTINUE

idummy=SetFont(FontXY)

idummy=GetFontInfo(FInf)

idummy=SetColor(TextColor)

CALL MoveTo(X0-GetGTextExtent('y=sin2(2x)+Vx-cos2(x/2)')/2+Lx/2, * Y0-Ly-FInf.PixHeight-TicSize,xy) CALL OutGText('y=sin2(2x)+Vx-cos2(x/2)') PAUSE 'Press ENTER for exit' idummy=SetVideoMode($DEFAULTMODE)

END