com.mxgraph.generatorfunction.mxGeneratorRandomFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jgraphx Show documentation
Show all versions of jgraphx Show documentation
JGraphX Swing Component - Java Graph Visualization Library
This is a binary & source redistribution of the original, unmodified JGraphX library originating from:
"https://github.com/jgraph/jgraphx/archive/v3.4.1.3.zip".
The purpose of this redistribution is to make the library available to other Maven projects.
package com.mxgraph.generatorfunction;
import com.mxgraph.view.mxCellState;
/**
* @author Mate
* A generator random cost function
* It will generate random (type "double") edge weights in the range of (minWeight, maxWeight) and rounds the values to roundToDecimals
*/
public class mxGeneratorRandomFunction extends mxGeneratorFunction
{
private double maxWeight = 1;
private double minWeight = 0;
private int roundToDecimals = 2;
public mxGeneratorRandomFunction(double minWeight, double maxWeight, int roundToDecimals)
{
setWeightRange(minWeight, maxWeight);
setRoundToDecimals(roundToDecimals);
};
public double getCost(mxCellState state)
{
Double edgeWeight = null;
edgeWeight = Math.random() * (maxWeight - minWeight) + minWeight;
edgeWeight = (double) Math.round(edgeWeight * Math.pow(10, getRoundToDecimals())) / Math.pow(10, getRoundToDecimals());
return edgeWeight;
};
public double getMaxWeight()
{
return maxWeight;
};
public void setWeightRange(double minWeight, double maxWeight)
{
this.maxWeight = Math.max(minWeight, maxWeight);
this.minWeight = Math.min(minWeight, maxWeight);
};
public double getMinWeight()
{
return minWeight;
};
public int getRoundToDecimals()
{
return roundToDecimals;
};
public void setRoundToDecimals(int roundToDecimals)
{
this.roundToDecimals = roundToDecimals;
};
};