Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,17 +1,17 @@
(import '[java.awt Color Graphics Dimension]
'[javax.swing JFrame JPanel])
(ns rosettacode.circle-random-points
(:import [java.awt Color Graphics Dimension]
[javax.swing JFrame JPanel]))
(let [points (->> (for [x (range -15 16)
y (range -15 16)
:when (<= 10 (Math/sqrt (+ (* x x) (* y y))) 15)]
(let [points (->> (for [x (range -15 16), y (range -15 16)
:when (<= 10 (Math/hypot x y) 15)]
[(+ x 15) (+ y 15)])
shuffle
(take 100 ,))]
(take 100))]
(doto (JFrame.)
(.add (doto (proxy [JPanel] []
(paint [^Graphics g]
(doseq [[x y] points]
(.fillRect g (* 10 x) (* 10 y) 10 10))))
(doseq [[x y] points]
(.fillRect g (* 10 x) (* 10 y) 10 10))))
(.setPreferredSize (Dimension. 310 310))))
(.setResizable false)
(.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)

View file

@ -0,0 +1,30 @@
defmodule Random do
def init() do
:random.seed(:erlang.now())
end
def generate_point() do
x = :random.uniform(31) - 16
y = :random.uniform(31) - 16
if 10*10 <= x*x + y*y and x*x + y*y <= 15*15 do
{x, y}
else
generate_point()
end
end
def circle() do
points = for _ <- 1..100, do: generate_point()
for x <- -15..15 do
for y <- -15..15 do
if Enum.member?(points, {x, y}) do
IO.write "x"
else
IO.write " "
end
end
IO.puts ""
end
end
end
Random.init()
Random.circle()

View file

@ -0,0 +1,17 @@
// Generate points in [min,max]^2 with constraint
function random_point (min, max, constraint)
[x, y] = [random(min, max), random(min, max)]
return constraint(x, y) ? [x, y] : random_point(min, max, constraint)
end
// Generate point list
in_circle = { x, y => 10**2 <= x**2 + y**2 and x**2 + y**2 <= 15**2 }
points = [].comp([0:100], {__ => random_point(-15, 15, in_circle)})
// Show points
for i in [-15:16]
for j in [-15:16]
>> [i, j] in points ? "x" : " "
end
>
end

View file

@ -0,0 +1,12 @@
a := table():
i := 1:
while i < 100 do
ba := (rand(-15 .. 15))():
bb := (rand(-15 .. 15))():
b := evalf(sqrt(ba^2+bb^2)):
if b <= 15 and b >= 10
then a[i] := [ba, bb]:
i := i+1:
end if:
end do:
plots:-pointplot(convert(a,list));

View file

@ -0,0 +1,43 @@
*process source attributed xref or(!);
annulus: procedure options (main);
/* version 1 does not handle (0/15) etc. this does. */
/* we show 1000 points here */
declare 1 point(10000),
2 x fixed binary,
2 y fixed binary;
declare (i, j, a, b, a2, b2, c) fixed binary(31);
j = 0;
do i = 1 to 1000;
r=rand(31); a=r-16;
r=rand(31); b=r-16;
a2=a*a;
b2=b*b;
c2=a2+b2;
if c2>= 100 & c2 <= 225 then
do; j = j + 1; x(j) = a; y(j) = b;
/* put Edit(a,b,c)(3(F(3))); */ end;
end;
/* PLOT */
declare table(-15:15, -15:15) character (2);
table = ' ';
do i = 1 to j;
table(x(i), y(i)) = '*';
end;
do i = -15 to 15;
put skip;
do j = -15 to 15;
put edit (table(i,j)) (a);
end;
end;
rand: Proc(n) Returns(Bin Fixed(31));
/*--------------------------------------------------------------------
* Return a random integer between 1 and n
*-------------------------------------------------------------------*/
Dcl r Bin Float(31);
Dcl (n,d) Bin Fixed(31);
r=random();
d=r*n+1;
Return(d);
End;
End annulus;

View file

@ -1,25 +1,28 @@
/*REXX program gens 100 random points in an annulus: 10 ≤ √(x²y²) ≤ 15 */
arg points low high .; $= /*allow args from command line. */
if points=='' then points=100
if low=='' then low=10; low2= low**2
if high=='' then high=15; high2=high**2
do x=-high; x2=x*x /*gen all possible annulus points*/
if x<0 & x2>high2 then iterate
if x>0 & x2>high2 then leave
do y=-high; y2=y*y; s=x2+y2
if (y<0 & s>high2) | s<low2 then iterate
if y>0 & s>high2 then leave
$=$ x','y
/*REXX program gens 100 random points in an annulus: 10 ≤ √(x²+y²) ≤ 15 */
parse arg points low high . /*allow parms from command line. */
if points=='' then points=100
if low=='' then low=10; low2= low**2 /*define a square shortcut.*/
if high=='' then high=15; high2=high**2 /* " " " " */
$=
do x=-high; x2=x*x /*gen all possible annulus points*/
if x<0 & x2>high2 then iterate
if x>0 & x2>high2 then leave
do y=-high; y2=y*y; s=x2+y2
if (y<0 & s>high2) | s<low2 then iterate
if y>0 & s>high2 then leave
$=$ x','y /*add a point-set to the $ list. */
end /*y*/
end /*x*/
field.=; plotChar='O'; minY=high2; maxY=-minY; ap=words($)
plotChar='O'; minY=high2; maxY=-minY; ap=words($); field.=
do j=1 for points /*"draw" the x,y points [char O].*/
parse value word($,random(1,ap)) with x ',' y /*pick random point.*/
do j=1 for points /*"draw" the x,y points [char O].*/
parse value word($,random(1,ap)) with x ',' y /*pick random point.*/
field.y=overlay(plotChar, field.y, x+high+1) /*"draw: the point. */
minY=min(minY,y); maxY=max(maxY,y) /*plot restricting. */
end /*j*/
do y=minY to maxY; if field.y=='' then iterate; say field.y; end /*plot*/
do y=minY to maxY /*display the annulus to screen. */
if field\=='' then say field.y /*Not blank? Then display it. */
end /*y*/
/*stick a fork in it, we're done.*/

View file

@ -1,25 +1,28 @@
/*REXX program gens 100 random points in an annulus: 10 ≤ √(x²y²) ≤ 15 */
arg points low high .; $= /*allow args from command line. */
if points=='' then points=100
if low=='' then low=10; low2= low**2
if high=='' then high=15; high2=high**2
do x=-high; x2=x*x /*gen all possible annulus points*/
if x<0 & x2>high2 then iterate
if x>0 & x2>high2 then leave
do y=-high; y2=y*y; s=x2+y2
if (y<0 & s>high2) | s<low2 then iterate
if y>0 & s>high2 then leave
$=$ x','y
/*REXX program gens 100 random points in an annulus: 10 ≤ √(x²+y²) ≤ 15 */
parse arg points low high . /*allow parms from command line. */
if points=='' then points=100
if low=='' then low=10; low2= low**2 /*define a square shortcut.*/
if high=='' then high=15; high2=high**2 /* " " " " */
$=
do x=-high; x2=x*x /*gen all possible annulus points*/
if x<0 & x2>high2 then iterate
if x>0 & x2>high2 then leave
do y=-high; y2=y*y; s=x2+y2
if (y<0 & s>high2) | s<low2 then iterate
if y>0 & s>high2 then leave
$=$ x','y /*add a point-set to the $ list. */
end /*y*/
end /*x*/
field.=; plotChar='O'; minY=high2; maxY=-minY; ap=words($)
plotChar='O'; minY=high2; maxY=-minY; ap=words($); field.=
do j=1 for points /*"draw" the x,y points [char O].*/
parse value word($,random(1,ap)) with x ',' y /*pick random point.*/
field.y=overlay(plotChar, field.y, x+high+1) /*"draw: the point. */
do j=1 for points /*"draw" the x,y points [char O].*/
parse value word($,random(1,ap)) with x ',' y /*pick random point.*/
field.y=overlay(plotChar, field.y, 2*x+2*high+1) /*"draw: the point. */
minY=min(minY,y); maxY=max(maxY,y) /*plot restricting. */
end /*j*/
do y=minY to maxY; if field.y=='' then iterate; say field.y; end /*plot*/
do y=minY to maxY /*display the annulus to screen. */
if field\=='' then say field.y /*Not blank? Then display it. */
end /*y*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,40 @@
/* REXX ---------------------------------------------------------------
* show 100 random points of an annulus with radius 10 to 15
* 18.06.2014 Walter Pachl 'derived/simplified' from REXX version 1
*--------------------------------------------------------------------*/
Parse Arg points low high scale . /* allow parms from command line.*/
If points=='' Then points=100 /* number of points */
If low=='' Then low=10 /* inner radius */
If high=='' Then high=15 /* outer radius */
If scale=='' Then scale=2 /* horizontal scaling */
low2=low**2
high2=high**2
/* first compute all possible points */
point.=0
Do x=-high To high
x2=x*x
Do y=-high To high
y2=y*y
s=x2+y2
If s>=low2 &s<=high2 Then Do
z=point.0+1
point.z=x y
point.0=z
End
End
End
plotchar='O'
line.=''
np=point.0 /* available points */
Do j=1 To points /* pick the needed points */
r=random(1,np)
Parse Var point.r x y /* coordinates */
line.y=overlay(plotchar,line.y,scale*(x+high)+1) /* put into line*/
point.r=point.np /* replace taken point by last*/
np=np-1 /* reduce available points */
If np=0 Then Leave /* all possible points taken */
End
/* now draw the picture */
Do y=-high To high
Say line.y
End

View file

@ -0,0 +1,78 @@
/* REXX ---------------------------------------------------------------
* 19.06.2014 Walter Pachl alternate algorithm
* the idea: yl is a list of y coordinates which may have unused points
* one of the y's is picked at random
* Then we look for unused x coordinates in this line
* we pick one at random or drop the y from yl if none is found
* When yl becomes empty, all points are used and we stop
*--------------------------------------------------------------------*/
Parse Arg n r rr scale
If r='' Then r=10
If rr='' Then rr=15
If n='' Then n=100
If scale='' Then scale=2
r2=r*r
rr2=rr*rr
ymin=0
ymax=rr*2
ol=''
pp.=0
used.=0
yl='' /* list of available y values */
Do y=-rr To rr
yl=yl y
End
Do Until pp.0=n /*look for the required points*/
If yl='' Then Do /* no more points available */
Say 'all points filled'
Leave
End
yi=random(1,words(yl)) /* pick a y */
y=word(yl,yi)
y2=y*y
p.=0
Do x=0 To rr /* Loop through possible x's */
x2=x*x
xy2=x2+y2
If xy2>=r2&xy2<=rr2 Then Do /* within the annulus */
Call take x y
Call take (-x) y
End
End
If p.0>0 Then Do /* some x's found (or just 1) */
xi=random(1,p.0) /* pick an x */
z=pp.0+1
pp.z=p.xi
pp.0=z
Parse Var pp.z xa ya
used.xa.ya=1 /* remember it's taken */
End
Else Do /* no x for this y */
yi=wordpos(y,yl) /* remove y from yl */
Select
When yi=1 Then yl=subword(yl,yi+1)
When yi=words(yl) Then yl=subword(yl,1,yi-1)
Otherwise yl=subword(yl,1,yi-1) subword(yl,yi+1)
End
End
End
line.='' /* empty the raster */
Do i=1 To pp.0 /* place the points */
Parse Var pp.i x y
line.y=overlay('+',line.y,scale*(rr+x)+1)
End
Do y=-rr To rr /* show the result */
Say line.y
End
say pp.0 'points filled'
Exit
Return
take: Procedure Expose p. used. /* add x to p. if its not used*/
Parse Arg x y
If used.x.y=0 Then Do
z=p.0+1
p.z=x y
p.0=z
End
Return

View file

@ -1,13 +1,13 @@
points = (1...100).map {
# choose a random radius and angle
angle = rand * 2.0 * Math::PI
rad = rand * 5.0 + 10.0
# convert back from polar to cartesian coordinates
[rad * Math::cos(angle), rad * Math::sin(angle)].map(&:round)
}
points = (1..100).map do
# choose a random radius and angle
angle = rand * 2.0 * Math::PI
rad = rand * 5.0 + 10.0
# convert back from polar to cartesian coordinates
[rad * Math::cos(angle), rad * Math::sin(angle)].map(&:round)
end
(-15..15).each do |row|
puts((-15..15).map { |col| points.include?([row, col]) ? "X" : " " }.join)
puts (-15..15).map { |col| points.include?([row, col]) ? "X" : " " }.join
end
load 'raster_graphics.rb'

View file

@ -0,0 +1,10 @@
r2 = 10*10..15*15
range = (-15..15).to_a
points = range.product(range).each_with_object([]) do |(i,j), pt|
pt << [i,j] if r2.cover?(i*i + j*j)
end
puts "Precalculate: #{points.size}"
pt = Hash.new(" ")
points.sample(100).each{|i,j| pt[[i,j]] = " o"}
puts range.map{|i| range.map{|j| pt[[i,j]]}.join}

View file

@ -1,73 +1,77 @@
object CRP extends SimpleSwingApplication {
import scala.swing._
import scala.swing.Swing._
import scala.swing.{MainFrame, Panel, SimpleGUIApplication}
import scala.swing.event._
import java.awt.{Color, Dimension, Graphics, Graphics2D, Point, geom}
import scala.util.Random
import scala.math._
import java.awt.{ Color, geom,Graphics2D ,Rectangle}
import scala.math.hypot
import scala.swing.{MainFrame,Panel,SimpleSwingApplication}
import scala.swing.Swing.pair2Dimension
import scala.util.Random
object CirculairConstrainedRandomPoints extends SimpleSwingApplication {
//min/max of display-x resp. y
val dx0,dy0 = 30
val dxm,dym = 430
val dx0, dy0 = 30; val dxm, dym = 430
val prefSizeX, prefSizeY = 480
val prefSizeX,prefSizeY = 480
val palet = 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 color
val xmax, ymax = 20; val xmin, ymin = -xmax
lazy val ui = new Panel {
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
preferredSize = (prefSizeX, prefSizeY)
val xmax,ymax = 20; val xmin,ymin = -20
case class Coord(x: Double, y: Double) {
val dx = (((dxm-dx0)/2+x.toDouble/xmax*(dxm-dx0)/2)+dx0).toInt
val dy = (((dym-dy0)/2-y.toDouble/ymax*(dym-dy0)/2)+dy0).toInt
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
}
case class Circle(x: Double, y: Double, r: Double, c: java.awt.Color) {
val mdp = Coord(x,y)
val dr = (Coord(r,0).dx-pcentre.dx)*2
val dx = mdp.dx-dr/2
val dy = mdp.dy-dr/2
object Circle {
def apply(x: Double, y: Double, r: Double, color: Color) =
new Circle(Coord(x, y), r, color)
}
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)
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:
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(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)})
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,cm(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)}
val circles = cs.map { case (x, y, r, c) => Circle(x, y, r, palet(c)) }
override def paintComponent(g: Graphics2D) = {
super.paintComponent(g)
circles.foreach{c=>{g.setColor(c.c); g.drawOval(c.dx,c.dy,c.dr,c.dr)}}
g.setColor(cm("r")); points.foreach(g.draw(_))
g.setColor(cm("s")); g.draw(a_path)
xlabels.foreach{case (text,px,py)=>g.drawString(text,px,py)}
ylabels.foreach{case (text,px,py)=>g.drawString(text,px,py)}
circles.foreach { c => { g.setColor(c.color); g.drawOval(c.dx, c.dy, c.dr, c.dr) } }
g.setColor(palet("r")); points.foreach(g.draw(_))
g.setColor(palet("s")); g.draw(a_path)
xlabels.foreach { case (text, px, py) => g.drawString(text, px, py) }
ylabels.foreach { case (text, px, py) => g.drawString(text, px, py) }
}
}
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 ui
def top = new MainFrame {
title = "Rosetta Code >>> Task: Constrained random points on a circle | Language: Scala"