绘图功能没有达到我的预期

时间:2014-06-25 17:45:50

标签: java plot

在下面的代码中,我有一个CentralPoints类和一个我要绘制的RandomPunt类。 我试图通过使用以下代码来绘制它。

package opdracht1;

import java.awt.Color;
import java.awt.Dimension;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.math.plot.*;
import javax.swing.*;

/**
 *
 * @author Falko & Daniël
 */
public class Opdracht1_1 
{
    private static Opdracht1_1 op = new Opdracht1_1();
    private int k = 2;

    private double[] puntenX = {1.0,1.5,3.0,5.0,3.5,4.5,3.5};
    private double[] puntenY = {1.0,2.0,4.0,7.0,5.0,5.0,4.5};

    private double xmin = 0;
    private double xmax = 5;
    private double ymin = 0;
    private double ymax = 7;

    private List<CentralPoints> centralpoints;

    public static void main(String[] args)
    {
        op.vindcentralepunten(3, 0.0, 0.0, 5.0, 7.0, 7);
    }
    public void plotGraph(int count) 
    {
        Color[] colors = {Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow, Color.black};

        // create your PlotPanel (you can use it as a JPanel)
        Plot2DPanel plot = new Plot2DPanel();

        double[] centralX = new double[centralpoints.size()], centralY = new double[centralpoints.size()];
        for(int i = 0; i < centralpoints.size(); i++) 
        {
            centralX[i] = centralpoints.get(i).getX();
            centralY[i] = centralpoints.get(i).getY();
        }
        for(int i=0;i<centralpoints.size();i++) //centralpoints.size()
        {
            try 
            {
                double[] targetX = new double[centralpoints.get(i).getList().size()];
                double[] targetY = new double[centralpoints.get(i).getList().size()];

                for(int j = 0; j < centralpoints.get(i).getList().size(); j++)
                {
                    targetX[j] = centralpoints.get(i).getList().get(j).getX();
                    targetY[j] = centralpoints.get(i).getList().get(j).getY();
                }
                //System.out.println(targetX.length+" lengthh "+targetY.length);
                //System.out.println(targetX[0]+" hallooooo "+targetY[0]); //1.0 1.0

                System.out.println("Net voordat ik plot!");
                for(double x : targetX) System.out.println("x: "+x);

                for(double y : targetY) System.out.println("y: "+y);
                //System.out.println(targetX[1]+""+targetY[1]);
                switch(i) 
                {
                    case 0: plot.addScatterPlot("my plot", colors[0], targetX, targetY); break;
                    //case 1: plot.addScatterPlot("my plot", colors[9], targetX, targetY); break;
                    //case 2: plot.addScatterPlot("my plot", colors[4], targetX, targetY); break;
                    //case 3: plot.addScatterPlot("my plot", colors[11], targetX, targetY); break;
                    //case 4: plot.addScatterPlot("my plot", colors[6], targetX, targetY); break;
                }
            } 
            catch(NullPointerException npe) 
            {
                System.out.println(npe);
            }
        }
        //Voeg central punten toe
        //plot.addScatterPlot("my plot", colors[12], centralX, centralY);

        JFrame frame = new JFrame("plot panel: "+count);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);

        frame.setContentPane(plot);
        frame.setVisible(true);

    }
    public void vindcentralepunten(int k, double xmin, double ymin, double xmax, double ymax, int aantalwillekeurigePunten) 
    {
        List<RandomPunt> randompoints = new ArrayList<RandomPunt>();
        centralpoints = new ArrayList<CentralPoints>();

        Random rand = new Random();

        for(int i = 0; i<k; i++) 
        {
            CentralPoints cp = new CentralPoints();
            //cp.setx(rand.nextDouble()*(xmax-xmin)+1);
            //cp.sety(rand.nextDouble()*(ymax-ymin)+1);
            cp.setX(i+2);
            cp.setY(i+2);
            centralpoints.add(cp);
        }
        for(int j = 0; j<puntenX.length; j++) 
        {
            RandomPunt rp = new RandomPunt();
            rp.setX(puntenX[j]);
            rp.setY(puntenY[j]);
            randompoints.add(rp);
        }

        Boolean[] done = new Boolean[centralpoints.size()];
        for(int i = 0; i < centralpoints.size(); i++) 
        {
            done[i] = false;
        }
        int count = 1;
        double[] oldx = new double[centralpoints.size()],
                oldy = new double[centralpoints.size()];
        for(RandomPunt rp : randompoints)
        {
            double smallest = 10000000.0, totaleafstand = 0.0;
            CentralPoints smallestCp = null, current = null;
            for(CentralPoints cp : centralpoints)
            {
                try { if(cp.getList().contains(rp)) current = cp;}
                catch(NullPointerException npe){}
                if(pythagoras(rp.getX(),rp.getY(),cp.getX(),cp.getY())<smallest) 
                {
                    smallest = pythagoras(rp.getX(),rp.getY(),cp.getX(),cp.getY());
                    smallestCp = cp;
                    totaleafstand = totaleafstand + pythagoras(rp.getX(), rp.getY(), cp.getX(), cp.getY());
                }
            }
            double afstand = smallestCp.getAfstand() + pythagoras(rp.getX(), rp.getY(), smallestCp.getX(), smallestCp.getY());
            smallestCp.setAfstand(afstand);

            List<RandomPunt> list = new ArrayList<RandomPunt>();
            if(smallestCp.getList()!=null) 
            {
                list = smallestCp.getList();
            }
            if(current!=null) current.getList().remove(rp);
            list.add(rp);
            smallestCp.setList(list);
        }
        while(Arrays.asList(done).contains(false)) 
        {
            op.plotGraph(count);
            for(int centp = 0; centp < centralpoints.size(); centp++) 
            {
                try 
                {
                    double smallestx = 10000000.0,
                            smallesty = 10000000.0,
                            biggestx = 0.0,
                            biggesty = 0.0;
                    for(RandomPunt randp : centralpoints.get(centp).getList()) 
                    {
                        if(smallestx > randp.getX()) smallestx=randp.getX();
                        if(smallesty > randp.getY()) smallesty=randp.getY();
                        if(biggestx < randp.getX()) biggestx=randp.getX();
                        if(biggesty < randp.getY()) biggesty=randp.getY();
                    }
                    double x = (biggestx+smallestx)/2;
                    double y = (biggesty+smallesty)/2;
                    System.out.println(biggestx+"+"+smallestx+"/2="+x);
                    System.out.println(biggesty+"+"+smallesty+"/2="+y);

                    if(oldx[centp] == x && oldy[centp] == y) done[centp] = true;
                    centralpoints.get(centp).setX(x);
                    centralpoints.get(centp).setY(y);
                    oldx[centp] = x;
                    oldy[centp] = y;
                } 
                catch(NullPointerException npe) 
                {
                    done[centp] = true;
                }
            }
            count++;
        }
        //op.plotGraph(count);
    }
    public double pythagoras(double rpx, double rpy, double cpx, double cpy) { return Math.sqrt(Math.pow((rpx-cpx), 2)+Math.pow((rpy-cpy), 2)); }
}

现在我希望在以下坐标处得到分数(randompunt):

x:1.0 y:1.0
x:1.5 y:2.0
x:3.0 y:4.0
x:5.0 y:7.0
x:3.5 y:5.0
x:4.5 y:5.0
x:3.5 y:4.5

我们现在遇到的奇怪的事情和真正的问题是前2个点(randompunt)被放置在错误的位置,它们被放置在这些坐标上:

x:1.0 y:1.5
x:1.0 y:2.0

有人看到代码中出错的地方吗?:)

非常感谢您的帮助

1 个答案:

答案 0 :(得分:1)

你在第149行有一个非常可疑的陈述:

list.add(rp);

其中rp是遍历列表randompoints的循环变量,该列表已填充puntenXpuntenY的坐标对。

我认为这些要点应保持不变;因此,将其中一个实例传递到一个列表中,该列表最终会出现在可能发生事情的另一组对象中:复制操作会更安全。

相关问题