1 /**
2  * Copyright (c) 2007-2017, JGraph Ltd
3  */
4 package com.mxgraph.analysis;
5 
6 import com.mxgraph.util.mxPoint;
7 import com.mxgraph.view.mxCellState;
8 
9 /**
10  * Implements a cost function for the Euclidean length of an edge.
11  */
12 public class mxDistanceCostFunction implements mxICostFunction
13 {
14 
15 	/**
16 	 * Returns the Euclidean length of the edge defined by the absolute
17 	 * points in the given state or 0 if no points are defined.
18 	 */
getCost(mxCellState state)19 	public double getCost(mxCellState state)
20 	{
21 		double cost = 0;
22 		int pointCount = state.getAbsolutePointCount();
23 
24 		if (pointCount > 0)
25 		{
26 			mxPoint last = state.getAbsolutePoint(0);
27 
28 			for (int i = 1; i < pointCount; i++)
29 			{
30 				mxPoint point = state.getAbsolutePoint(i);
31 				cost += point.getPoint().distance(last.getPoint());
32 				last = point;
33 			}
34 		}
35 
36 		return cost;
37 	}
38 }
39