Constrained random points on a circle: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: oops, totally misread the task. My bad.)
Line 2,078: Line 2,078:


=={{header|Scala}}==
=={{header|Scala}}==
[[Category:Scala Implementations]]{{libheader|Scala}}<lang Scala>import java.awt.{ Color, geom,Graphics2D ,Rectangle}
{{works with|Scala|2.9.1}}
import scala.math.hypot
<lang Scala>object CRP extends SimpleSwingApplication {
import scala.swing._
import scala.swing.{MainFrame,Panel,SimpleSwingApplication}
import scala.swing.Swing._
import scala.swing.Swing.pair2Dimension
import scala.swing.{MainFrame, Panel, SimpleGUIApplication}
import scala.util.Random
import scala.swing.event._
import java.awt.{Color, Dimension, Graphics, Graphics2D, Point, geom}
import scala.util.Random
import scala.math._


object CirculairConstrainedRandomPoints extends SimpleSwingApplication {
//min/max of display-x resp. y
//min/max of display-x resp. y
val dx0,dy0 = 30
val dx0, dy0 = 30; val dxm, dym = 430
val dxm,dym = 430
val prefSizeX, prefSizeY = 480

val palet = Map("b" -> Color.blue, "g" -> Color.green, "r" -> Color.red, "s" -> Color.black)
val prefSizeX,prefSizeY = 480
val cs = List((0, 0, 10, "b"), (0, 0, 15, "g")) //circle position and color
lazy val ui = new Panel {
val xmax, ymax = 20; val xmin, ymin = -xmax

class Coord(x: Double, y: Double) {
def dx = (((dxm - dx0) / 2 + x.toDouble / xmax * (dxm - dx0) / 2) + dx0).toInt
def dy = (((dym - dy0) / 2 - y.toDouble / ymax * (dym - dy0) / 2) + dy0).toInt
}

object Coord {
def apply(x: Double, y: Double) = new Coord(x, y)
}

//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).toSeq.
map { case (x, y, r) => new Rectangle(Coord(x, y).dx - 2, Coord(x, y).dy - 2, 4, 4) }

private def ui = new Panel {
background = Color.white
background = Color.white
preferredSize = (prefSizeX, prefSizeY)
preferredSize = (prefSizeX, prefSizeY)


val xmax,ymax = 20; val xmin,ymin = -20
class Circle(center: Coord, r: Double, val color: Color) {
val dr = (Coord(r, 0).dx - pcentre.dx) * 2
val dx = center.dx - dr / 2
val dy = center.dy - dr / 2
}


object Circle {
case class Coord(x: Double, y: Double) {
def apply(x: Double, y: Double, r: Double, color: Color) =
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 mdp = Coord(x,y)
val pxmax = Coord(xmax, 0); val pxmin = Coord(xmin, 0)
val dr = (Coord(r,0).dx-pcentre.dx)*2
val pymax = Coord(0, ymax); val pymin = Coord(0, ymin)
val dx = mdp.dx-dr/2
val dy = mdp.dy-dr/2
}
val pcentre = Coord(0,0)
val pxmax = Coord(xmax,0); val pxmin = Coord(xmin,0)
val pymax = Coord(0,ymax); val pymin = Coord(0,ymin)


//axes:
//axes:
var a_path = new geom.GeneralPath
val a_path = new geom.GeneralPath
a_path.moveTo(pxmin.dx, pxmin.dy); a_path.lineTo(pxmax.dx, pxmax.dy) //x-axis
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
a_path.moveTo(pymin.dx, pymin.dy); a_path.lineTo(pymax.dx, pymax.dy) //y-axis


//labeling:
//labeling:
val labels = List(-20,-15,-10,-5,5,10,15,20)
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 { 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)}}
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 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)})
val ylabels = labels.map(y => { val p = Coord(0, y); Triple(y.toString, p.dx - 20, p.dy + 5) })


//circles:
//circles:
val circles = cs.map{case (x,y,r,c)=>Circle(x,y,r,cm(c))}
val circles = cs.map { case (x, y, r, c) => Circle(x, y, r, palet(c)) }


//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) = {
override def paintComponent(g: Graphics2D) = {
super.paintComponent(g)
super.paintComponent(g)
circles.foreach{c=>{g.setColor(c.c); g.drawOval(c.dx,c.dy,c.dr,c.dr)}}
circles.foreach { c => { g.setColor(c.color); g.drawOval(c.dx, c.dy, c.dr, c.dr) } }
g.setColor(cm("r")); points.foreach(g.draw(_))
g.setColor(palet("r")); points.foreach(g.draw(_))
g.setColor(cm("s")); g.draw(a_path)
g.setColor(palet("s")); g.draw(a_path)
xlabels.foreach{case (text,px,py)=>g.drawString(text,px,py)}
xlabels.foreach { case (text, px, py) => g.drawString(text, px, py) }
ylabels.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 {
def top = new MainFrame {