1. #1

    Registriert seit
    19.11.2011
    Beiträge
    496
    Thanked 412 Times in 268 Posts

    Standard Java - Rechtshändiges Koordinatensystem

    Hallo,

    bei dem Koordinatensystem in Java liegt der Ursprung (0,0) in der linken oberen Ecke. Die x-Achse wächst nach rechts und die y-Achse wächst nach unten. Ist ja auch logisch, wenn man weiß, wie etwas auf dem Bildschirm dargestellt wird. Darstellen von negativen y-Koordinaten ist so weiteres nicht möglich. (negative x-Koordinaten schon, führen aber nicht zum erwarteten Ergebnis).
    Die x-Achse des typischen Koordinatensystems, das man aus dem Mathematikunterricht kennt, wächst ebenfalls nach rechts. Die y-Achse allerdings nach oben.Negative Koordinaten sind darstellbar.

    Das habe ich versucht in Java zu implementieren, u. a. um schneller mit Mathematikhausaufgaben fertig zu werden.

    Die Klasse:
    Code:
    package j3d.graphics3d;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class MathGraphics {
        // Interface für Mathematische Funktionen
        public interface Function {
            double f(double x);     // Liefert den y-Wert, wenn Eingabe = x-Wert
        }
        
        private Graphics g;             // Grafikkontext
        private int width, height;      // Breite und Höhe des "grafischen Bereiches"
        private double xStep, yStep;    // x-, y-Schritte
        private int xSpacing, ySpacing; // x-, y-Pixelabstand
        
        /**
         * Konstruktor.
         * Initialisiert Member.
         * @param g Grafikkontext
         * @param width Breite
         * @param height Höhe
         */
        public MathGraphics(Graphics g, int width, int height) {
            this.g = g;
            this.width = width;
            this.height = height;
        }
        
        /**
         * Konstruktor.
         * Initialisiert Member und setzt den Ursprung.
         * @param g Grafikkontext
         * @param originX x-Koordinate des Ursprungs
         * @param originY y-Koordinate des Ursprungs
         * @param width Breite 
         * @param height Höhe
         */
        public MathGraphics(Graphics g, int originX, int originY, int width, int height) {
            this(g, width, height);
            setOrigin(originX, originY);
        }
        
        /**
         * Kosntruktor.
         * Initialisiert Member, setzt den Ursprung, die Intervalle und die Abstände
         * @param g Grafikkontext
         * @param originX x-Koordinate des Ursprungs
         * @param originY y-Koordinate des Ursprungs
         * @param width Breite
         * @param height Höhe
         * @param xStep Schritte (x-Achse)
         * @param yStep Schritte (y-Achse)
         * @param xSpacing (grafischer) Abstand der x-Werte
         * @param ySpacing (grafischer) Abstand der y-Werte
         */
        public MathGraphics(Graphics g, int originX, int originY, int width, int height,
                            double xStep, double yStep, int xSpacing, int ySpacing) {
            this(g, originX, originY, width, height);
            setSteps(xStep, yStep);
            setSpacing(xSpacing, ySpacing);
        }
        
        /**
         * Zeichnet eine Funktion
         * @param function Funktion
         */
        public void drawFunction(MathGraphics.Function function) {
            for(int i = -width / 2; i <= width / 2; i++) {
                double fx = (double) i / xSpacing * xStep;
                double fy = function.f(fx);
                int y = (int) (fy * ySpacing / yStep);
                int i2 = i + 1;
                double fx2 = (double) i2 / xSpacing * xStep;
                double fy2 = function.f(fx2);
                int y2 = (int) (fy2 * ySpacing / yStep);
                g.drawLine(i, -y, i2, -y2);
            }
        }
        
        /**
         * Zeichnet eine Funktion in einer beliebigen Farbe
         * @param function Funktion
         * @param color Farbe
         */
        public void drawFunction(MathGraphics.Function function, Color color) {
            g.setColor(color);
            drawFunction(function);
            g.setColor(Color.black);
        }
        
        /**
         * Zeichnet das Raster
         */
        public void drawRaster() {
            g.setColor(Color.gray);
            // 1.Quadrant
            for(int x = xSpacing; x <= width / 2; x += xSpacing) {
                for(int y = -ySpacing; y >= -height / 2; y -= ySpacing) {
                    g.drawRect(x, y, 1, 1);
                }
            }
            // 2.Quadrant
            for(int x = -xSpacing; x >= -width / 2; x -= xSpacing) {
                for(int y = -ySpacing; y >= -height / 2; y -= ySpacing) {
                    g.drawRect(x, y, 1, 1);
                }
            }
            // 3.Quadrant
            for(int x = -xSpacing; x >= -width / 2; x -= xSpacing) {
                for(int y = ySpacing; y <= height / 2; y += ySpacing) {
                    g.drawRect(x, y, 1, 1);
                }
            }
            // 4.Quadrant
            for(int x = xSpacing; x <= width / 2; x += xSpacing) {
                for(int y = ySpacing; y <= height / 2; y += ySpacing) {
                    g.drawRect(x, y, 1, 1);
                }
            }
            g.setColor(Color.black);
        }
        
        /**
         * Zeichnet beide Achsen
         * @param label Wenn true, bekommen die einzelnen Schritte Ziffern zugewiesen
         */
        public void drawAxes(boolean label) {
            drawXAxis(label);
            drawYAxis(label);
        }
        
        /**
         * Zeichnet die X-Achse
         * @param label Wenn true, bekommen die einzelnen Schritte Ziffern zugewiesen
         */
        public void drawXAxis(boolean label) {
            g.setColor(Color.black);
            double xSt = -xStep;
            g.drawLine(-width / 2, 0, width / 2, 0);
            g.drawLine(width / 2 - 10, -5, width / 2, 0);
            g.drawLine(width / 2 - 10, 5, width / 2, 0);
            for(int i = 0; i >= -width / 2; i -= xSpacing) {
                g.drawLine(i, -5, i, 5);
                if(label && i != 0) {
                    int strW = g.getFontMetrics(g.getFont()).stringWidth(String.valueOf(i));
                    int strH = g.getFontMetrics(g.getFont()).getHeight();
                    g.drawString(String.valueOf(xSt), i - strW / 2, 5 + strH);
                    xSt += -xStep;
                }
            }
            xSt = xStep;
            for(int i = 0; i <= width / 2; i += xSpacing) {
                g.drawLine(i, -5, i, 5);
                if(label && i != 0) {
                    int strW = g.getFontMetrics(g.getFont()).stringWidth(String.valueOf(i));
                    int strH = g.getFontMetrics(g.getFont()).getHeight();
                    g.drawString(String.valueOf(xSt), i - strW / 2, 5 + strH);
                    xSt += xStep;
                }
            }
        }
        
        /**
         * Zeichnet die Y-Achse
         * @param label Wenn true, bekommen die einzelnen Schritte Ziffern zugewiesen
         */
        public void drawYAxis(boolean label) {
            g.setColor(Color.black);
            double ySt = yStep;
            g.drawLine(0, -height / 2, 0, height / 2);
            g.drawLine(-5, -height / 2 + 10, 0, -height / 2);
            g.drawLine(5, -height / 2 + 10, 0, -height / 2);
            for(int i = 0; i >= -height / 2; i -= ySpacing) {
                g.drawLine(-5, i, 5, i);
                if(label && i != 0) {
                    int strH = g.getFontMetrics(g.getFont()).getHeight();
                    g.drawString(String.valueOf(ySt), 10, i + strH / 4);
                    ySt += yStep;
                }
            }
            ySt = -yStep;
            for(int i = 0; i <= height / 2; i += ySpacing) {
                g.drawLine(-5, i, 5, i);
                if(label && i != 0) {
                    int strH = g.getFontMetrics(g.getFont()).getHeight();
                    g.drawString(String.valueOf(ySt), 10, i + strH / 4);
                    ySt += -yStep;
                }
            }
        }
        
        /**
         * Setzt die "Schritte"
         * @param x Schritte auf der x-Achse
         * @param y Schritte auf der y-Achse
         */
        public void setSteps(double x, double y) {
            this.xStep = x;
            this.yStep = y;
        }
        
        /**
         * Setzt die (grafischen) Abstände
         * @param x Abstand auf der x-Achse
         * @param y Abstand auf der y-Achse
         */
        public void setSpacing(int x, int y) {
            this.xSpacing = x;
            this.ySpacing = y;
        }
        
        /**
         * Setzt den Ursprung
         * @param x x-Koordinate
         * @param y y-Koordinate
         */
        public void setOrigin(int x, int y) {
            g.translate(x, y);
        }
    }
    Beispielanwendung:
    Code:
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setFont(new Font("SansSerif", Font.PLAIN, 9));
                MathGraphics mg = new MathGraphics(
                        g,
                        getWidth() / 2 - 1, getHeight() / 2 - 1,
                        getWidth(), getHeight(),
                        1, 2.5,
                        40, 40);
                mg.drawAxes(true);
                //mg.drawRaster();
                
                g.setFont(new Font("SansSerif", Font.PLAIN, 12));
                g.setColor(Color.red);
                g.drawString("f(x) = x^3 - 3x^2 + 3x - 1", 30, -getHeight() / 2 + 20);
                mg.drawFunction(new MathGraphics.Function() {
                    @Override
                    public double f(double x) {
                        return Math.pow(x, 3) - 3 * Math.pow(x, 2) + 3 * x - 1;
                    }
                }, Color.red);
            }
    Screenshots:

    Klicke auf die Grafik für eine größere Ansicht 

Name:	beispielmitrastersqo2q.png 
Hits:	579 
Größe:	70,2 KB 
ID:	3431

    Klicke auf die Grafik für eine größere Ansicht 

Name:	beispielohnerastercap96.png 
Hits:	490 
Größe:	68,6 KB 
ID:	3432

  2. The Following 2 Users Say Thank You to Mr. White For This Useful Post:

    Mentos (13.01.2012), uncopyable (10.01.2012)

Ähnliche Themen

  1. Antworten: 8
    Letzter Beitrag: 02.12.2013, 23:41
  2. Java EE 32 Bit?
    Von Pwned im Forum Java
    Antworten: 2
    Letzter Beitrag: 04.08.2012, 23:08
  3. Ubuntu Sun Java
    Von hYpercrites im Forum Linux
    Antworten: 3
    Letzter Beitrag: 20.06.2012, 15:17
  4. [JAVA] KButton
    Von Mentos im Forum Sourcecode
    Antworten: 0
    Letzter Beitrag: 04.04.2012, 17:49
  5. Rechtshändiges Koordinatensystem
    Von GerMaN-DeLuXe im Forum Showroom
    Antworten: 0
    Letzter Beitrag: 10.01.2012, 22:24
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.