Anonymous user
Constrained random points on a circle: Difference between revisions
Constrained random points on a circle (view source)
Revision as of 17:43, 4 June 2014
, 10 years ago→{{header|Scala}}
(→{{header|Perl 6}}: oops, totally misread the task. My bad.) |
|||
Line 2,078:
=={{header|Scala}}==
[[Category:Scala Implementations]]{{libheader|Scala}}<lang Scala>import java.awt.{ Color, geom,Graphics2D ,Rectangle}
<lang Scala>object CRP extends SimpleSwingApplication {▼
▲ import scala.math._
//min/max of display-x resp. y
val dx0, dy0 = 30; val dxm, dym = 430
val
▲ }
object Coord {
def apply(x: Double, y: Double) = new Coord(x, y)
▲ }
val points =
private def ui = new Panel {
background = Color.white
preferredSize = (prefSizeX, prefSizeY)
val dr = (Coord(r, 0).dx - pcentre.dx) * 2
}▼
object Circle {
▲ case class Coord(x: Double, y: Double) {
▲ val dx = (((dxm-dx0)/2+x.toDouble/xmax*(dxm-dx0)/2)+dx0).toInt
new Circle(Coord(x, y), r, color)
▲ val dy = (((dym-dy0)/2-y.toDouble/ymax*(dym-dy0)/2)+dy0).toInt
}
val pcentre = Coord(0, 0)▼
▲ case class Circle(x: Double, y: Double, r: Double, c: java.awt.Color) {
val pxmax = Coord(xmax, 0); val
val pymax = Coord(0, ymax); val
▲ val dx = mdp.dx-dr/2
▲ val dy = mdp.dy-dr/2
▲ }
▲ val pcentre = Coord(0,0)
//axes:
a_path.moveTo(pxmin.dx, pxmin.dy); a_path.lineTo(pxmax.dx, pxmax.dy) //x-axis
a_path.moveTo(pymin.dx, pymin.dy); a_path.lineTo(pymax.dx, pymax.dy) //y-axis
//labeling:
val labels = List(-20, -15, -10, -5, 5, 10, 15, 20)
labels.foreach { x => { val p = Coord(x, 0); a_path.moveTo(p.dx, p.dy - 3); a_path.lineTo(p.dx, p.dy + 3) } }
labels.foreach { y => { val p = Coord(0, y); a_path.moveTo(p.dx - 3, p.dy); a_path.lineTo(p.dx + 3, p.dy) } }
val xlabels = labels.map(x => { val p = Coord(x, 0); Triple(x.toString, p.dx - 3, p.dy + 20) })
val ylabels = labels.map(y => { val p = Coord(0, y); Triple(y.toString, p.dx - 20, p.dy + 5) })
//circles:
val circles = cs.map { case (x, y, r, c) => Circle(x, y, r,
▲ //points:
▲ val points = new Iterator[Int] {val r = new Random; def next = r.nextInt(31)-15; def hasNext = true}.toStream
▲ .zip(new Iterator[Int] {val r = new Random; def next = r.nextInt(31)-15; def hasNext = true}.toStream)
▲ .map{case (x,y)=>(x,y,hypot(x,y))}.filter{case (x,y,r)=>r>=10&&r<=15}.take(100).toList
▲ .map{case (x,y,r) => new Rectangle(Coord(x,y).dx-2, Coord(x,y).dy-2, 4, 4)}
override def paintComponent(g: Graphics2D) = {
super.paintComponent(g)
circles.foreach { c => { g.setColor(c.
g.setColor(
g.setColor(
xlabels.foreach { case (text, px, py) => g.drawString(text, px, py) }
ylabels.foreach { case (text, px, py) => g.drawString(text, px, py) }
}
} // def ui
▲ val cm = Map("b"->Color.blue,"g"->Color.green,"r"->Color.red,"s"->Color.black)
▲ val cs = List((0,0,10,"b"),(0,0,15,"g")) //circle position and colour
def top = new MainFrame {
|