//planets is an exploration of scale func planets(width, height int, message string) { w := openvg.VGfloat(width) h := openvg.VGfloat(height) y := h / 2 margin := openvg.VGfloat(100.0) minsize := openvg.VGfloat(7.0) labeloc := openvg.VGfloat(100.0) bgcolor := "black" labelcolor := "white" maxsize := (h / 2) * 0.05 origin := sun.distance mostDistant := neptune.distance firstSize := mercury.radius lastSize := neptune.radius openvg.Start(width, height) openvg.BackgroundColor(bgcolor) for _, p := range solarSystem { x := vmap(p.distance, origin, mostDistant, margin, w-margin) r := vmap(p.radius, firstSize, lastSize, minsize, maxsize) if p.name == "Sun" { openvg.FillRGB(p.color.Red, p.color.Green, p.color.Blue, 1) openvg.Circle(margin-(r/2), y, r) } else { light(x, y, r, p.color) openvg.Circle(x, y, r) if p.name == "Saturn" { ringwidth := r * 2.35 // Saturn's rings are over 2x the planet radius openvg.StrokeWidth(3) openvg.StrokeRGB(p.color.Red, p.color.Green, p.color.Blue, 1) openvg.Line((x - ringwidth/2), y, (x + ringwidth/2), y) openvg.StrokeWidth(0) } } if p.name == "Earth" && len(message) > 1 { openvg.StrokeColor(labelcolor) openvg.StrokeWidth(1) openvg.Line(x, y+(r/2), x, y+labeloc) openvg.StrokeWidth(0) openvg.FillColor(labelcolor) openvg.TextMid(x, y+labeloc+10, message, "sans", 12) } } openvg.End() }
func main() { width, height := openvg.Init() w := openvg.VGfloat(width) h := openvg.VGfloat(height) y := h / 2 var ( margin openvg.VGfloat = 100.0 minsize openvg.VGfloat = 7.0 labeloc openvg.VGfloat = 100.0 ) bgcolor := "black" labelcolor := "white" maxsize := (h / 2) * 0.05 origin := sun.distance mostDistant := neptune.distance firstSize := mercury.radius lastSize := neptune.radius openvg.Start(width, height) openvg.BackgroundColor(bgcolor) for _, p := range solarSystem { x := vmap(p.distance, origin, mostDistant, margin, w-margin) r := vmap(p.radius, firstSize, lastSize, minsize, maxsize) if p.name == "Sun" { openvg.FillRGB(p.color.Red, p.color.Green, p.color.Blue, 1) openvg.Circle(margin-(r/2), y, r) } else { light(x, y, r, p.color) openvg.Circle(x, y, r) } if p.name == "Earth" && len(os.Args) > 1 { openvg.StrokeColor(labelcolor) openvg.StrokeWidth(1) openvg.Line(x, y+(r/2), x, y+labeloc) openvg.StrokeWidth(0) openvg.FillColor(labelcolor) openvg.TextMid(x, y+labeloc+10, os.Args[1], "sans", 12) } } openvg.End() bufio.NewReader(os.Stdin).ReadByte() openvg.Finish() }
func main() { var color string openvg.Start(width, height) openvg.Background(200, 200, 200) for i := 0; i < niter; i++ { x := random(0, width) y := random(height/3, (height*2)/3) r := random(0, 10000) switch { case r >= 0 && r <= 2500: color = "white" case r > 2500 && r <= 5000: color = "maroon" case r > 5000 && r <= 7500: color = "gray" case r > 7500 && r <= 10000: color = "black" } openvg.FillColor(color, openvg.VGfloat(opacity)) openvg.Circle(openvg.VGfloat(x), openvg.VGfloat(y), openvg.VGfloat(size)) } openvg.End() bufio.NewReader(os.Stdin).ReadByte() openvg.Finish() }
// rshapes draws shapes with random colors, openvg.Strokes, and sizes. func rshapes(width, height, n int) { var sx, sy, cx, cy, px, py, ex, ey, pox, poy openvg.VGfloat np := 10 polyx := make([]openvg.VGfloat, np) polyy := make([]openvg.VGfloat, np) openvg.Start(width, height) for i := 0; i < n; i++ { openvg.FillRGB(randcolor(), randcolor(), randcolor(), openvg.VGfloat(rand.Float32())) openvg.Ellipse(randf(width), randf(height), randf(200), randf(100)) openvg.Circle(randf(width), randf(height), randf(100)) openvg.Rect(randf(width), randf(height), randf(200), randf(100)) openvg.Arc(randf(width), randf(height), randf(200), randf(200), randf(360), randf(360)) sx = randf(width) sy = randf(height) openvg.StrokeRGB(randcolor(), randcolor(), randcolor(), 1) openvg.StrokeWidth(randf(5)) openvg.Line(sx, sy, sx+randf(200), sy+randf(100)) openvg.StrokeWidth(0) sx = randf(width) sy = randf(height) ex = sx + randf(200) ey = sy cx = sx + ((ex - sx) / 2.0) cy = sy + randf(100) openvg.Qbezier(sx, sy, cx, cy, ex, ey) sx = randf(width) sy = randf(height) ex = sx + randf(200) ey = sy cx = sx + ((ex - sx) / 2.0) cy = sy + randf(100) px = cx py = sy - randf(100) openvg.Cbezier(sx, sy, cx, cy, px, py, ex, ey) pox = randf(width) poy = randf(height) for j := 0; j < np; j++ { polyx[j] = pox + randf(200) polyy[j] = poy + randf(100) } openvg.Polygon(polyx, polyy) // , np) pox = randf(width) poy = randf(height) for j := 0; j < np; j++ { polyx[j] = pox + randf(200) polyy[j] = poy + randf(100) } openvg.Polyline(polyx, polyy) // , np) } openvg.FillRGB(128, 0, 0, 1) openvg.Text(20, 20, "OpenVG on the Raspberry Pi", "sans", 32) openvg.End() }
func draw(w, h openvg.VGfloat) { paintBG(w, h) var p particle var grav = openvg.VGfloat(gravity) for i := 0; i < nparticles; i++ { p = particles[i] openvg.FillRGB(p.r, p.g, p.b, 1) openvg.Circle(p.x, p.y, p.radius) // Apply the velocity p.x += p.vx p.y += p.vy p.vx *= 0.98 if p.vy > 0 { p.vy *= 0.97 } // Gravity p.vy -= grav // Stop p leaving the canvas if p.x < -50 { p.x = w + 50 } if p.x > w+50 { p.x = -50 } // When particle reaches the bottom of screen reset velocity & start posn if p.y < -50 { p.x = 0 p.y = 0 p.vx = openvg.VGfloat(rand.Intn(maxrand)%30) + 30 p.vy = openvg.VGfloat(rand.Intn(maxrand)%20) + 40 if directionRTL { p.vx *= -1 p.x = w } } if p.y > h+50 { p.y = -50 } particles[i] = p } openvg.End() }
// showgrid xrays a slide func showgrid(d deck.Deck, n int, p float64) { w := openvg.VGfloat(d.Canvas.Width) h := openvg.VGfloat(d.Canvas.Height) percent := openvg.VGfloat(p) fs := (w / 100) // labels are 1% of the width xpct := (percent / 100.0) * w ypct := (percent / 100.0) * h openvg.StrokeColor("lightgray", 0.5) openvg.StrokeWidth(3) // horizontal gridlines xl := percent for x := xpct; x <= w; x += xpct { openvg.Line(x, 0, x, h) openvg.Text(x, percent, fmt.Sprintf("%.0f%%", xl), "sans", int(fs)) xl += percent } // vertical gridlines yl := percent for y := ypct; y <= h; y += ypct { openvg.Line(0, y, w, y) openvg.Text(percent, y, fmt.Sprintf("%.0f%%", yl), "sans", int(fs)) yl += percent } // show boundary and location of images if n < 0 || n > len(d.Slide) { return } for _, im := range d.Slide[n].Image { x := pct(im.Xp, w) y := pct(im.Yp, h) iw := openvg.VGfloat(im.Width) ih := openvg.VGfloat(im.Height) if im.Scale > 0 { iw *= openvg.VGfloat(im.Scale / 100) ih *= openvg.VGfloat(im.Scale / 100) } openvg.FillRGB(127, 0, 0, 0.3) openvg.Circle(x, y, fs) openvg.FillRGB(255, 0, 0, 0.1) openvg.Rect(x-iw/2, y-ih/2, iw, ih) } openvg.End() }
func main() { width, height := openvg.Init() // OpenGL, etc initialization w2 := openvg.VGfloat(width / 2) h2 := openvg.VGfloat(height / 2) w := openvg.VGfloat(width) openvg.Start(width, height) // Start the picture openvg.BackgroundColor("black") // Black background openvg.FillRGB(44, 100, 232, 1) // Big blue marble openvg.Circle(w2, 0, w) // The "world" openvg.FillColor("white") // White text openvg.TextMid(w2, h2, "hello, world", "serif", width/10) // Greetings openvg.End() // End the picture bufio.NewReader(os.Stdin).ReadBytes('\n') // Pause until [RETURN] openvg.Finish() // Graphics cleanup }
func gradient(width, height int) { w := openvg.VGfloat(width) h := openvg.VGfloat(height) stops := []openvg.Offcolor{ {0.0, openvg.RGB{255, 255, 255}, 1.0}, {0.5, openvg.RGB{128, 128, 128}, 1.0}, {1.0, openvg.RGB{0, 0, 0}, 1.0}, } x1 := w / 8 x2 := (w * 3) / 8 y1 := h / 3 y2 := (h * 2) / 3 cx := (w * 3) / 4 cy := (h / 2) r := (x2 - x1) fx := cx + (r / 4) fy := cy + (r / 4) openvg.Start(width, height) openvg.BackgroundRGB(128, 128, 128, 1) openvg.FillLinearGradient(x1, y1, x2, y2, stops) openvg.Rect(x1, y1, x2-x1, y2-y1) openvg.FillRadialGradient(cx, cy, fx, fy, r, stops) openvg.Circle(cx, cy, r) openvg.FillRGB(0, 0, 0, 0.3) openvg.Circle(x1, y1, 10) openvg.Circle(x2, y2, 10) openvg.Circle(cx, cy, 10) openvg.Circle(cx+r/2, cy, 10) openvg.Circle(fx, fy, 10) openvg.FillColor("black") SansTypeface := "sans" openvg.TextMid(x1, y1-20, "(x1, y1)", SansTypeface, 18) openvg.TextMid(x2, y2+10, "(x2, y2)", SansTypeface, 18) openvg.TextMid(cx, cy, "(cx, cy)", SansTypeface, 18) openvg.TextMid(fx, fy, "(fx, fy)", SansTypeface, 18) openvg.TextEnd(cx+(r/2)+20, cy, "r", SansTypeface, 18) openvg.TextMid(x1+((x2-x1)/2), h/6, "Linear Gradient", SansTypeface, 36) openvg.TextMid(cx, h/6, "Radial Gradient", SansTypeface, 36) openvg.End() }
func main() { var nr = flag.Int("n", 500, "number of objects") var message = flag.String("m", "Go/OpenVG", "message") var bgcolor = flag.String("bg", "white", "background color") var fgcolor = flag.String("fg", "maroon", "text color") flag.Parse() rseed() width, height := openvg.Init() fw := openvg.VGfloat(width) fh := openvg.VGfloat(height) openvg.Start(width, height) openvg.BackgroundColor(*bgcolor) for i := 0; i < *nr; i++ { red := uint8(rand.Intn(255)) green := uint8(rand.Intn(255)) blue := uint8(rand.Intn(255)) alpha := randf() x := randf() * fw y := randf() * fh radius := randf() * fw / 10 openvg.FillRGB(red, green, blue, alpha) openvg.Circle(x, y, radius) } openvg.FillColor(*fgcolor) openvg.TextMid(fw/2, fh/2, *message, "sans", width/25) openvg.End() bufio.NewReader(os.Stdin).ReadBytes('\n') openvg.Finish() }
func main() { width, height := openvg.Init() // OpenGL, etc initialization w2 := openvg.VGfloat(width / 2) h2 := openvg.VGfloat(height / 2) w := openvg.VGfloat(width) stops := []openvg.Offcolor{ {0.0, openvg.RGB{44, 100, 232}, 1.0}, // blue-ish {0.5, openvg.RGB{22, 50, 151}, 1.0}, // darker blue {1.0, openvg.RGB{88, 200, 255}, 1.0}, // lighter blue } openvg.Start(width, height) // Start the picture openvg.BackgroundColor("black") // Black background openvg.FillRadialGradient(w2, 0, w2, w2, w*.5, stops) // Big blue marble openvg.Circle(w2, 0, w) // The "world" openvg.FillColor("white") // White text openvg.TextMid(w2, h2, "hello, world", "serif", width/10) // Greetings //openvg.SaveEnd("hvg.raw") // End the picture openvg.End() bufio.NewReader(os.Stdin).ReadBytes('\n') // Pause until [RETURN] openvg.Finish() // Graphics cleanup }
func main() { var ( filename = flag.String("f", "svgcolors.txt", "input file") fontname = flag.String("font", "sans", "fontname") neg = flag.Bool("neg", false, "negative") showrgb = flag.Bool("rgb", false, "show RGB") showcode = flag.Bool("showcode", true, "show colors and codes") circsw = flag.Bool("circle", true, "circle swatch") outline = flag.Bool("outline", false, "outline swatch") fontsize = flag.Int("fs", 12, "fontsize") rowsize = flag.Int("r", 32, "rowsize") colw = flag.Float64("c", 340, "column size") swatch = flag.Float64("s", 16, "swatch size") gutter = flag.Float64("g", 12, "gutter") err error tcolor, line string ) flag.Parse() f, oerr := os.Open(*filename) if oerr != nil { fmt.Fprintf(os.Stderr, "%v\n", oerr) return } width, height := openvg.Init() openvg.Start(width, height) fw := openvg.VGfloat(width) fh := openvg.VGfloat(height) if *neg { openvg.FillColor("black") openvg.Rect(0, 0, fw, fh) tcolor = "white" } else { openvg.FillColor("white") openvg.Rect(0, 0, fw, fh) tcolor = "black" } top := fh - 32.0 left := openvg.VGfloat(32.0) cw := openvg.VGfloat(*colw) sw := openvg.VGfloat(*swatch) g := openvg.VGfloat(*gutter) in := bufio.NewReader(f) for x, y, nr := left, top, 0; err == nil; nr++ { line, err = in.ReadString('\n') fields := strings.Split(strings.TrimSpace(line), "\t") if nr%*rowsize == 0 && nr > 0 { x += cw y = top } if len(fields) == 3 { var red, green, blue uint8 fmt.Sscanf(fields[2], "%d,%d,%d", &red, &green, &blue) openvg.FillRGB(red, green, blue, 1) if *outline { openvg.StrokeColor("black") openvg.StrokeWidth(1) } if *circsw { openvg.Circle(x+sw/2, y+sw/2, sw) } else { openvg.Rect(x, y, sw, sw) } openvg.StrokeWidth(0) openvg.FillColor(tcolor) openvg.Text(x+sw+openvg.VGfloat(*fontsize/2), y, fields[0], *fontname, *fontsize) var label string if *showcode { if *showrgb { label = fields[1] } else { label = fields[2] } openvg.FillColor("gray") openvg.TextEnd(x+cw-(sw+g), y, label, *fontname, *fontsize) } } y -= (sw + g) } openvg.End() bufio.NewReader(os.Stdin).ReadBytes('\n') openvg.Finish() }
// plot places a plot at the specified location with the specified dimemsions // using the specified settings, using the specified data func plot(x, y, w, h openvg.VGfloat, settings plotset, d []rawdata) { nd := len(d) if nd < 2 { fmt.Fprintf(os.Stderr, "%d is not enough points to plot\n", len(d)) return } // Compute the minima and maxima of the data maxx, minx := d[0].x, d[0].x maxy, miny := d[0].y, d[0].y for _, v := range d { if v.x > maxx { maxx = v.x } if v.y > maxy { maxy = v.y } if v.x < minx { minx = v.x } if v.y < miny { miny = v.y } } // Prepare for a area or line chart by allocating // polygon coordinates; for the horizon plot, you need two extra coordinates // for the extrema. needpoly := settings.opt["area"] || settings.opt["connect"] var xpoly, ypoly []openvg.VGfloat if needpoly { xpoly = make([]openvg.VGfloat, nd+2) ypoly = make([]openvg.VGfloat, nd+2) // preload the extrema of the polygon, // the bottom left and bottom right of the plot's rectangle xpoly[0] = x ypoly[0] = y xpoly[nd+1] = x + w ypoly[nd+1] = y } // Draw the plot's bounding rectangle if settings.opt["showbg"] && !settings.opt["sameplot"] { openvg.FillColor(settings.attr["bgcolor"]) openvg.Rect(x, y, w, h) } // Loop through the data, drawing items as specified spacer := openvg.VGfloat(10.0) for i, v := range d { xp := fmap(v.x, minx, maxx, x, x+w) yp := fmap(v.y, miny, maxy, y, y+h) if needpoly { xpoly[i+1] = xp ypoly[i+1] = yp } if settings.opt["showbar"] { openvg.StrokeColor(settings.attr["barcolor"]) openvg.StrokeWidth(settings.size["barsize"]) openvg.Line(xp, yp, xp, y) } if settings.opt["showdot"] { openvg.FillColor(settings.attr["dotcolor"]) openvg.StrokeWidth(0) openvg.Circle(xp, yp, settings.size["dotsize"]) } if settings.opt["showx"] { if i%int(settings.size["xinterval"]) == 0 { openvg.FillColor("black") openvg.TextMid(xp, y-(spacer*2), fmt.Sprintf("%d", int(v.x)), settings.attr["font"], int(settings.size["fontsize"])) openvg.StrokeColor("silver") openvg.StrokeWidth(1) openvg.Line(xp, y, xp, y-spacer) } openvg.StrokeWidth(0) } } // Done constructing the points for the area or line plots, display them in one shot if settings.opt["area"] { openvg.FillColor(settings.attr["areacolor"]) openvg.Polygon(xpoly, ypoly) } if settings.opt["connect"] { openvg.StrokeColor(settings.attr["linecolor"]) openvg.StrokeWidth(settings.size["linesize"]) openvg.Polyline(xpoly[1:nd+1], ypoly[1:nd+1]) } // Put on the y axis labels, if specified if settings.opt["showy"] { bot := openvg.VGfloat(math.Floor(float64(miny))) top := openvg.VGfloat(math.Ceil(float64(maxy))) yrange := top - bot interval := yrange / openvg.VGfloat(settings.size["yinterval"]) for yax := bot; yax <= top; yax += interval { yaxp := fmap(yax, bot, top, openvg.VGfloat(y), openvg.VGfloat(y+h)) openvg.FillColor("black") openvg.TextEnd(x-spacer, yaxp, fmt.Sprintf("%.1f", yax), settings.attr["font"], int(settings.size["fontsize"])) openvg.StrokeColor("silver") openvg.StrokeWidth(1) openvg.Line(x-spacer, yaxp, x, yaxp) } openvg.StrokeWidth(0) } // Finally, tack on the label, if specified if len(settings.attr["label"]) > 0 { openvg.FillColor(settings.attr["labelcolor"], 0.3) openvg.TextMid(x+(w/2), y+(h/2), settings.attr["label"], settings.attr["font"], int(w/8)) // int(settings.size["fontsize"])) } openvg.StrokeWidth(0) }
// refcard shows a reference card of shapes func refcard(width, height int) { shapenames := []string{ "Circle", "Ellipse", "Rectangle", "Rounded Rectangle", "Line", "Polyline", "Polygon", "Arc", "Quadratic Bezier", "Cubic Bezier", "Image", } top := openvg.VGfloat(height) * .95 sx := openvg.VGfloat(width) * 0.10 sy := top sw := openvg.VGfloat(width) * .05 sh := openvg.VGfloat(height) * .045 dotsize := openvg.VGfloat(7.0) spacing := openvg.VGfloat(2.0) fontsize := int(openvg.VGfloat(height) * .033) shapecolor := Color{202, 225, 255, 1.0} openvg.Start(width, height) openvg.FillRGB(128, 0, 0, 1) openvg.TextEnd(openvg.VGfloat(width-20), openvg.VGfloat(height/2), "OpenVG on the Raspberry Pi", "sans", fontsize+(fontsize/2)) openvg.FillRGB(0, 0, 0, 1) for _, s := range shapenames { openvg.Text(sx+sw+sw/2, sy, s, "sans", fontsize) sy -= sh * spacing } sy = top cx := sx + (sw / 2) ex := sx + sw openvg.FillRGB(shapecolor.red, shapecolor.green, shapecolor.blue, shapecolor.alpha) openvg.Circle(cx, sy, sw) coordpoint(cx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Ellipse(cx, sy, sw, sh) coordpoint(cx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Rect(sx, sy, sw, sh) coordpoint(sx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Roundrect(sx, sy, sw, sh, 20, 20) coordpoint(sx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.StrokeWidth(1) openvg.StrokeRGB(204, 204, 204, 1) openvg.Line(sx, sy, ex, sy) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(ex, sy, dotsize, shapecolor) sy -= sh px := []openvg.VGfloat{sx, sx + (sw / 4), sx + (sw / 2), sx + ((sw * 3) / 4), sx + sw} py := []openvg.VGfloat{sy, sy - sh, sy, sy - sh, sy} openvg.Polyline(px, py) // , 5) coordpoint(px[0], py[0], dotsize, shapecolor) coordpoint(px[1], py[1], dotsize, shapecolor) coordpoint(px[2], py[2], dotsize, shapecolor) coordpoint(px[3], py[3], dotsize, shapecolor) coordpoint(px[4], py[4], dotsize, shapecolor) sy -= sh * spacing py[0] = sy py[1] = sy - sh py[2] = sy - (sh / 2) py[3] = py[1] - (sh / 4) py[4] = sy openvg.Polygon(px, py) // , 5) sy -= (sh * spacing) + sh openvg.Arc(sx+(sw/2), sy, sw, sh, 0, 180) coordpoint(sx+(sw/2), sy, dotsize, shapecolor) sy -= sh * spacing var cy, ey openvg.VGfloat cy = sy + (sh / 2) ey = sy openvg.Qbezier(sx, sy, cx, cy, ex, ey) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(cx, cy, dotsize, shapecolor) coordpoint(ex, ey, dotsize, shapecolor) sy -= sh * spacing ey = sy cy = sy + sh openvg.Cbezier(sx, sy, cx, cy, cx, sy, ex, ey) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(cx, cy, dotsize, shapecolor) coordpoint(cx, sy, dotsize, shapecolor) coordpoint(ex, ey, dotsize, shapecolor) sy -= (sh * spacing * 1.5) openvg.Image(sx, sy, 110, 110, "starx.jpg") openvg.End() }
// coordpoint marks a coordinate, preserving a previous color func coordpoint(x, y, size openvg.VGfloat, c Color) { openvg.FillRGB(128, 0, 0, 0.3) openvg.Circle(x, y, size) openvg.FillRGB(c.red, c.green, c.blue, c.alpha) }