Vector Feld erzeugen

Eichelhäer

Bekanntes Mitglied
Hallo,

ich bin gerade dabei ein Vectorfeld zu erzeugen. Nur komme ich beim rendern nicht weiter. Die Initialisierung ist kein Problem wie man einen Pfeil zeichnet auch aber mehrere Vectoren die in eine gewünschte Richtung zeigen das bekomme ich nicht hin. wäre für hilfe dankbar.

Hier mal die FlowField klasse:

Code:
public class FlowField {

    private int cols = 10;
    private int rows = 10;
    private Vector[][] flowfield = null;
    
    public FlowField() {
        this.flowfield = new Vector[rows][cols];
        init();
    }
    
    public void init() {
        
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
                flowfield[i][j] = new Vector(i,j);
            }
        }
        
    }
    
    public void drawFlowField(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        
    }
}
 

Blender3D

Top Contributor
Nur komme ich beim rendern nicht weiter.
VektorFeld.png
Hier ein Beispiel wie Du das Rendering machen kannst.
Java:
import javax.swing.JFrame;
import gui.VectorFieldPanel;

public class start {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Vektor Feld");
        frame.add(new VectorFieldPanel(500, 500));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.pack();
        frame.setVisible(true);
    }

}
Java:
public class Vector {
    public final float x;
    public final float y;

    public Vector(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public Vector add(Vector v) {
        return new Vector(x + v.x, y + v.y);
    }

    public float lenght() {
        return (float) Math.sqrt(x * x + y * y);
    }

    public Vector mul(float scale) {
        return new Vector(x * scale, y * scale);
    }

    public Vector norm() {
        float len = lenght();
        if (len == 0)
            throw new ArithmeticException("can't norm a null-vector");
        return this.mul(1f / len);
    }

    public Vector normalVector() {
        return new Vector(y, -x);
    }

    public Vector sub(Vector v) {
        return new Vector(v.x - x, v.y - y);
    }

    @Override
    public String toString() {
        return String.format("<%.2f|%.2f>", x, y);
    }

}
Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;

import math.Vector;

public class Arrow {
    private Vector pos;
    private Vector dir;
    private float lenght;

    public Arrow(Vector pos, Vector dir, float lenght) {
        this.pos = pos;
        this.lenght = lenght;
        this.dir = dir;
    }

    public void draw(Graphics g, Point origin, float scale) {
        Graphics2D g2 = (Graphics2D) g;
        Point start = toScreenPos(origin, pos, scale);
        Vector vEnd = pos.add(dir.mul(lenght));
        Point end = toScreenPos(origin, vEnd, scale);
        g.setColor(Color.black);
        g.drawLine(start.x, start.y, end.x, end.y);
        if (dir.lenght() == 0)
            return;
        Vector back = dir.norm().mul(-scale * 0.004f);       
        
        Vector backNormal = back.normalVector();
        Vector endArrow = vEnd.add(back);
        Vector vB = endArrow.add(backNormal.mul(-1));
        Vector vC = endArrow.add(backNormal);
        Point b = toScreenPos(origin, vB, scale);
        Point c = toScreenPos(origin, vC, scale);
        Polygon p = new Polygon();
        p.addPoint(b.x, b.y);
        p.addPoint(end.x, end.y);
        p.addPoint(c.x, c.y);
        g2.fillPolygon(p);
    }

    public static Point toScreenPos(Point origin, Vector pos, float scale) {
        Point p = new Point(origin.x, origin.y);
        Vector v = pos.mul(scale);
        p.x += Math.round(v.x);
        p.y += Math.round(-v.y);
        return p;
    }

    @Override
    public String toString() {
        return pos + " -> " + dir + String.format(" %.1f", lenght);
    }

}
Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import math.Vector;

public class VectorFlowField {
    private final static int[][] sign = { { 1, 1 }, { -1, -1 }, { -1, 1 }, { 1, -1 } };
    private final static int aX = 0; // y = a*x+b*y+c
    private final static int bX = 1;
    private final static int cX = 2;
    private final static int aY = 3;
    private final static int bY = 4;
    private final static int cY = 5;
    private int width = 500;
    private int height = 500;
    private float[] factor = new float[] { 1, 0,0, 0, 0,-1 };
    private float lenght = .2f;
    private float step = 1;
    private float scale = 40f;

    public void draw(Graphics g, Point origin) {
        drawCoordinateRaster(g, origin);
        g.setColor(Color.DARK_GRAY);
        int maxX = getmaxX();
        int maxY = getMaxY();
        for (float x = 0f; x < maxX; x += step) {
            for (float y = 0f; y < maxY; y += step) {
                for (int i = 0; i < sign.length; i++) {
                    Vector pos = new Vector(x * sign[i][0], y * sign[i][1]);
                    Vector dir = new Vector(getXvalue(x, y) * sign[i][0], getYvalue(x, y) * sign[i][1]);
                    Arrow arrow = new Arrow(pos, dir, lenght);
                    arrow.draw(g, origin, scale);
                }
            }
        }
    }

    private int getMaxY() {
        return (int) (height / scale / 2);
    }

    private int getmaxX() {
        return (int) (width / scale / 2);
    }

    private void drawCoordinateRaster(Graphics g, Point origin) {
        g.setColor(Color.BLUE);
        int maxX = getmaxX();
        int maxY = getMaxY();
        g.drawLine((int) (origin.x - (maxX + 1) * scale), origin.y, (int) (origin.x + (maxX + 1) * scale), origin.y);
        g.drawLine(origin.x, (int) (origin.y - (maxY + 1) * scale), origin.x, (int) (origin.y + (maxY + 1) * scale));

        int size = 4;
        int fontSize = (int) (g.getFont().getSize() * 1.1);
        for (int x = 1; x < maxX; x++) {
            for (int y = 1; y < maxY; y++) {
                for (int i = 0; i < sign.length; i++) {
                    int x1 = toScreenPos(origin.x, x * sign[i][0]);
                    int y1 = toScreenPos(origin.y, y * sign[i][1]);
                    g.drawLine(x1, origin.y - size, x1, origin.y + size);
                    g.drawLine(origin.x - size, y1, origin.x + size, y1);
                    x1 = (int) (x * scale * sign[i][0]) + origin.x;
                    y1 = (int) (y * scale * sign[i][1]) + origin.y;
                    g.drawString(x + "", x1, origin.y - fontSize);
                    g.drawString(y + "", origin.x + fontSize, y1);

                }
            }
        }
    }

    private int toScreenPos(int origin, float pos) {
        return origin + (int) (pos * scale);
    }

    private float getXvalue(float x, float y) {
        return (factor[aX] * x + factor[bX] * y)+factor[cX];
    }

    private float getYvalue(float x, float y) {
        return (factor[aY] * x + factor[bY] * y+factor[cY]);
    }
    public void setDimension( int width , int height) {
        this.width = width;
        this.height = height;
    }

    public void setLenghtFactor(float lenght) {
        this.lenght = lenght;
    }

    public void setStep(float step) {
        this.step = step;
    }

}
Java:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class VectorFieldPanel extends JPanel {
    private VectorFlowField field = new VectorFlowField();

    public VectorFieldPanel(int width, int height) {
        setPreferredSize(new Dimension(width, height));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        field.draw(g, new Point(getWidth() / 2, getHeight() / 2));
    }
}
 

Ähnliche Java Themen


Oben