Sunteți pe pagina 1din 4

Universitatea Tehnic a Moldovei

Facultatea Calculatoare, Informatic i Microelectronic


Catedra Calculatoare

RAPORT
Lucrare de laborator nr.3
la Grafica pe Calculator
Tema: DESENAREA GRAFICELOR PENTRU FUNCII

A efectuat: st. gr. C-151

Dutcovici Radu

A verificat: lector universitar

Plotnic Constantin

Chiinu 2017
Scopul lucrarii: Obinerea cunotinelor practice n sintezarea i afiarea graficelor funciilor n
baza bibliotecii grafice.

Varianta
12. y arcsin( k x); y 2 * sin( x); y tg (k x)

Sarcina lucrrii:
1. De a elabora un program pentru sintezarea i afiarea graficelor funciilor conform variantelor
(tabela 1)
2. S se foloseasc transformarea fereastr-poart.
3. De a prezenta imagini n diferite pori de vizualizare

Listingul programului:
package GP.lab3;

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {


MyJPanel panel;
MyFrame() {
this.setSize(1000, 700);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

this.panel = new MyJPanel();


this.add(panel);

this.setVisible(true);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}

class MyJPanel extends JPanel {


private Function f1 = x -> Math.asin(10*x);
private Function f2 = x -> 2*Math.sin(x);
private Function f3 = x -> Math.tan(10*x);

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.init(g);
Rect window;
window = new Rect(-1, -10,1, 30);
this.plotFunction(g, 1, f1, window);
window = new Rect(-2 * Math.PI, -4,2 * Math.PI, 4);
this.plotFunction(g, 3, f2, window);
window = new Rect(-Math.PI, -10,Math.PI, 10);
this.plotFunction(g, 4, f3, window);
}
private void init(Graphics g) {
this.setBackground(Color.BLACK);
int width = this.getWidth();
int height = this.getHeight();
g.drawLine(width / 2, 0, width / 2, height);
g.drawLine(0, height / 2, width, height / 2);
g.setColor(Color.green);
g.drawString("Lucrare de laborator: Nr3", 20, 50);
g.drawString("Tema: Transformarea Fereastra-Poata", 20, 80);
g.drawString("st. gr. C-151 Dutcovici Radu", 20, 110);
g.drawString("f1 = arcsin(k*x)", this.getWidth() / 2 + 10, this.getHeight() / 2 - 20);
g.drawString("f2 = 2*sin(x)", 0 + 10, this.getHeight() - 20);
g.drawString("f3 = tg(k*x) ", this.getWidth() / 2+ 10, this.getHeight() - 20);

}
private void plotFunction(Graphics g, int part, Function func, Rect window) {
int gxm = part == 2 || part == 3 ? 0 : this.getWidth() / 2 ;
int gym = part == 1 || part == 2 ? 0 : this.getHeight() / 2 ;
System.out.println(gxm + " " + gym);
Rect gate = new Rect(gxm, gym, gxm + this.getWidth() / 2, gym + this.getHeight() / 2);

double sx = (gate.xM - gate.xm) / (window.xM - window.xm);


double sy = (gate.yM - gate.ym) / (window.yM - window.ym);
double tx = gate.xm - window.xm * sx;
double dx = (window.xM - window.xm) / (gate.xM - gate.xm);

System.out.printf("dx: %s sx: %s sy: %s tx: %s%n", dx, sx, sy, tx);

int oxp = new Double(window.xm * sx + tx).intValue();


int oyp = new Double(gate.yM - func.f(window.xm) * sy + window.ym * sy).intValue();

g.setColor(Color.RED);
g.drawLine( new Double(window.xm * sx + tx).intValue(),
new Double(gate.yM + window.ym * sy).intValue(),
new Double(window.xM * sx + tx).intValue(),
new Double(gate.yM + window.ym * sy).intValue());
g.drawLine( new Double(tx).intValue(),
new Double(gate.yM - window.ym * sy + window.ym * sy).intValue(),
new Double(tx).intValue(),
new Double(gate.yM - window.yM * sy + window.ym * sy).intValue());

g.setColor(Color.blue);
for (double i = window.xm; i < window.xM; i += dx) {
int xp = new Double(i * sx + tx).intValue();
int yp = new Double(gate.yM - func.f(i) * sy + window.ym * sy).intValue();
System.out.printf("xp: %d yp: %d %f %f%n", xp, yp, i, func.f(i));

if (oyp >= gate.ym && oyp <= gate.yM && yp >= gate.ym && yp <= gate.yM)
g.drawLine(oxp, oyp, xp, yp);
oxp = xp;
oyp = yp;
}
}
}

class Rect {
double xm;
double ym;
double xM;
double yM;
Rect(double xm, double ym, double xM, double yM) {
this.xm = xm;
this.ym = ym;
this.xM = xM;
this.yM = yM;
}
}

interface Function{
Double f(double x);
}

Rezultatul programului:

Concluzii: n cadrul acestei lucrri de laborator am obinut cunotine practice n


sintezarea i afiarea graficelor funciilor n baza bibliotecii grafice.Am elaborat
un program pentru sintezarea i afiarea graficelor funciilor.De asemenea am
utilizat transformarea fereastr-poart,am prezentat ntr-un panel imaginile
graficelor funciilor n diferite pori de vizualizare.Am realizat n ntregime sarcina
propus de profesor.

S-ar putea să vă placă și