Plot coordinate pairs: Difference between revisions
Content added Content deleted
m (→{{header|Java}}: Correct Lang tag) |
(→{{header|Scala}}: Maintained) |
||
Line 1,386: | Line 1,386: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<lang Scala>import scala.swing. |
<lang Scala>import scala.swing.Swing.pair2Dimension |
||
import scala.swing. |
import scala.swing.{ MainFrame, Panel, Rectangle } |
||
import |
import java.awt.{ Color, Graphics2D, geom } |
||
import scala.swing.event._ |
|||
import java.awt.{Color, Dimension, Graphics, Graphics2D, Point, geom} |
|||
object PlotCoordPairs extends SimpleSwingApplication { |
object PlotCoordPairs extends scala.swing.SimpleSwingApplication { |
||
//min/max of display-x resp. y |
//min/max of display-x resp. y |
||
val dx0 = 70 |
val (dx0, dy0) = (70, 30) |
||
val |
val (dxm, dym) = (670, 430) |
||
val dxm = 670 |
|||
val |
val (prefSizeX, prefSizeY) = (720, 480) |
||
val prefSizeX = 720 |
|||
val prefSizeY = 480 |
|||
lazy val ui = new Panel { |
lazy val ui = new Panel { |
||
⚫ | |||
⚫ | |||
import math._ |
import math._ |
||
val xmax = { |
val xmax = { |
||
val f1 = pow(10,log10(xs.max).toInt) |
val f1 = pow(10, log10(xs.max).toInt) |
||
val f2 = if (f1<10) 10 else round(xs.max/f1)*f1 |
val f2 = if (f1 < 10) 10 else round(xs.max / f1) * f1 |
||
if (f2 >= xs.max) f2 else (round(xs.max/f1)+1)*f1 |
if (f2 >= xs.max) f2 else (round(xs.max / f1) + 1) * f1 |
||
} |
} |
||
val ymax = { |
val ymax = { |
||
val f1 = pow(10,log10(ys.max).toInt) |
val f1 = pow(10, log10(ys.max).toInt) |
||
val f2 = if (f1<10) 10 else round(ys.max/f1)*f1 |
val f2 = if (f1 < 10) 10 else round(ys.max / f1) * f1 |
||
if (f2 >= ys.max) f2 else (round(ys.max/f1)+1)*f1 |
if (f2 >= ys.max) f2 else (round(ys.max / f1) + 1) * f1 |
||
} |
} |
||
⚫ | |||
val yinterv = ymax/xs.size |
val (xinterv, yinterv) = (xmax / xs.size, ymax / xs.size) |
||
⚫ | |||
case class Coord(x: Double, y: Double) { |
case class Coord(x: Double, y: Double) { |
||
val dx = (x/xmax*(dxm-dx0)+dx0).toInt |
val (dx, dy) = ((x / xmax * (dxm - dx0) + dx0).toInt, (dym - y / ymax * (dym - dy0)).toInt) |
||
val dy = (dym-y/ymax*(dym-dy0)).toInt |
|||
} |
} |
||
⚫ | |||
val pcentre = Coord(0,0) |
val pcentre = Coord(0, 0) |
||
val pxmax = Coord(xmax,0) |
val pxmax = Coord(xmax, 0) |
||
val pymax = Coord(0, ymax) |
val pymax = Coord(0, ymax) |
||
⚫ | |||
⚫ | |||
//axes: |
//axes: |
||
val a_path = new geom.GeneralPath |
|||
a_path.moveTo( |
a_path.moveTo(pxmax.dx, pxmax.dy) |
||
a_path.lineTo( |
a_path.lineTo(pcentre.dx, pcentre.dy) //x-axis |
||
⚫ | |||
a_path.lineTo(pymax.dx, pymax.dy) //y-axis |
a_path.lineTo(pymax.dx, pymax.dy) //y-axis |
||
// interval |
// interval ticks: |
||
xs.map(i => Coord(i * xinterv, 0)).map(p => { |
|||
a_path.moveTo(p.dx, p.dy) |
|||
a_path.lineTo(p.dx, p.dy + 5) |
|||
⚫ | |||
xs.map(i => Coord(0, i * yinterv)).map(p => { |
|||
⚫ | |||
a_path.lineTo(p.dx - 5, p.dy) |
|||
⚫ | |||
//grid: |
//grid: |
||
val g_path = new geom.GeneralPath |
|||
(1 to xs.size). |
|||
⚫ | |||
map(i => Coord(i * xinterv, 0)).map(p => { |
|||
g_path.moveTo(p.dx, p.dy); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
g_path.moveTo(p.dx, p.dy); |
|||
g_path.lineTo(Coord(xmax, p.y).dx, Coord(xmax, p.y).dy) |
|||
⚫ | |||
//labeling: |
//labeling: |
||
val xlabels = (0 to xs.size).map(i=>{ |
val xlabels = (0 to xs.size).map(i => { |
||
val p = Coord(i * xinterv, 0) |
|||
⚫ | |||
Triple(p.x.toInt.toString, p.dx - 3, p.dy + 20) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
val p = Coord(0, i * yinterv) |
|||
Triple(p.y.toInt.toString, p.dx - 30, p.dy + 5) |
|||
}) |
|||
//curve: |
//curve: |
||
val path = new geom.GeneralPath |
|||
val curve = |
val curve = xs.map(i => Coord(xs(i), ys(i))) |
||
path.moveTo(curve |
path.moveTo(curve.head.dx, curve.head.dy) |
||
curve.map(p=>path.lineTo(p.dx,p.dy)) |
curve.map(p => path.lineTo(p.dx, p.dy)) |
||
//...flag all function values: |
//...flag all function values: |
||
val rects = curve.map(p=>new Rectangle(p.dx-3, p.dy-3, 6, 6)) |
val rects = curve.map(p => new Rectangle(p.dx - 3, p.dy - 3, 6, 6)) |
||
⚫ | |||
override def paintComponent(g: Graphics2D) = { |
override def paintComponent(g: Graphics2D) = { |
||
super.paintComponent(g) |
super.paintComponent(g) |
||
Line 1,464: | Line 1,477: | ||
g.setColor(Color.black) |
g.setColor(Color.black) |
||
g.draw(a_path) |
g.draw(a_path) |
||
xlabels.map(t=>g.drawString(t._1,t._2,t._3)) |
xlabels.map(t => g.drawString(t._1, t._2, t._3)) |
||
ylabels.map(t=>g.drawString(t._1,t._2,t._3)) |
ylabels.map(t => g.drawString(t._1, t._2, t._3)) |
||
g.draw(path) |
g.draw(path) |
||
rects.map(g.draw(_)) |
rects.map(g.draw(_)) |
||
} |
} |
||
} |
} |
||
val xs = |
val xs = 0 to 9 |
||
val ys = List(2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0) |
val ys: List[Double] = List(2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0) |
||
def top = new MainFrame { |
def top = new MainFrame { |
||
Line 1,478: | Line 1,491: | ||
contents = ui |
contents = ui |
||
} |
} |
||
}</lang> |
}</lang> |
||