Documente Academic
Documente Profesional
Documente Cultură
Lab3 GC Radu
Lab3 GC Radu
RAPORT
Lucrare de laborator nr.3
la Grafica pe Calculator
Tema: DESENAREA GRAFICELOR PENTRU FUNCII
Dutcovici Radu
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.*;
this.setVisible(true);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
@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);
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: