Here is the code:

```
import org.jfree.chart._
import org.jfree.data.xy._
object Main {
val t: Array[Double] = (0 until 1000).map(_ * 0.001).toArray
val x0 = 4.0
val y0 = 1.0
val x1 = 28.0
val y1 = 48.0
val x2 = 50.0
val y2 = 42.0
val x3 = 40.0
val y3 = 5.0
val termsZipped: Array[(((Double, Double), Double), Double)] = term(t, 0, 3).zip(term(t, 1, 2)).zip(term(t, 2, 1)).zip(term(t, 3, 0))
val xCoordinates = termsZipped.map {
case (((t0, t1), t2), t3) => x0 * t0 + 3 * x1 * t1 + 3 * x2 * t2 + x3 * t3
}
val yCoordinates = termsZipped.map {
case (((t0, t1), t2), t3) => y0 * t0 + 3 * y1 * t1 + 3 * y2 * t2 + y3 * t3
}
// add control points
val xCoordinatesAll = Array(x1, x0) ++ xCoordinates ++ Array(x3, x2)
val yCoordinatesAll = Array(y1, y0) ++ yCoordinates ++ Array(y3, y2)
// t^0 * (1 - t)^3
// t^1 * (1 - t)^2
// etc
def term(a: Array[Double], coef1: Double, coef2: Double): Array[Double] = {
a.map((x: Double) => Math.pow(x, coef1) * Math.pow(1 - x, coef2))
}
def main(args: Array[String]) {
val dataset = new DefaultXYDataset
dataset.addSeries("S", Array(xCoordinatesAll, yCoordinatesAll))
val frame = new ChartFrame(
"Title",
ChartFactory.createXYLineChart(
"Plot",
"X", "Y", dataset,
org.jfree.chart.plot.PlotOrientation.VERTICAL,
false, false, false
)
)
frame.pack()
frame.setVisible(true)
}
}
```

## 0 comments:

Post a Comment