From 875b1c7a15f289f388a0187781c5f2b1f4e9dc90 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Sun, 29 Jun 2014 14:14:19 -0400 Subject: Polygon function and demonstration --- fpdf_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'fpdf_test.go') diff --git a/fpdf_test.go b/fpdf_test.go index c727087..262e482 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -21,6 +21,7 @@ import ( "code.google.com/p/gofpdf" "fmt" "io/ioutil" + "math" "os" "path/filepath" "strings" @@ -1197,3 +1198,50 @@ func ExampleFpdf_tutorial24() { // Output: // Successfully generated pdf/tutorial24.pdf } + +// This example displays equilateral polygons in a demonstration of the Polygon +// function. +func ExampleFpdf_tutorial25() { + const rowCount = 5 + const colCount = 4 + const ptSize = 36 + var x, y, radius, gap, advance float64 + var rgVal int + var pts []gofpdf.PointType + vertices := func(count int) (res []gofpdf.PointType) { + var pt gofpdf.PointType + res = make([]gofpdf.PointType, 0, count) + mlt := 2.0 * math.Pi / float64(count) + for j := 0; j < count; j++ { + pt.Y, pt.X = math.Sincos(float64(j) * mlt) + res = append(res, gofpdf.PointType{x + radius*pt.X, y + radius*pt.Y}) + } + return + } + pdf := gofpdf.New("P", "mm", "A4", cnFontDir) // A4 210.0 x 297.0 + pdf.AddPage() + pdf.SetFont("Helvetica", "", ptSize) + pdf.SetDrawColor(0, 80, 180) + gap = 12.0 + pdf.SetY(gap) + pdf.CellFormat(190.0, gap, "Equilateral polygons", "", 1, "C", false, 0, "") + radius = (210.0 - float64(colCount+1)*gap) / (2.0 * float64(colCount)) + advance = gap + 2.0*radius + y = 2*gap + pdf.PointConvert(ptSize) + radius + rgVal = 230 + for row := 0; row < rowCount; row++ { + pdf.SetFillColor(rgVal, rgVal, 0) + rgVal -= 12 + x = gap + radius + for col := 0; col < colCount; col++ { + pts = vertices(row*colCount + col + 3) + // pdf.Circle(x, y, radius, "FD") + pdf.Polygon(pts, "FD") + x += advance + } + y += advance + } + pdf.OutputAndClose(docWriter(pdf, 25)) + // Output: + // Successfully generated pdf/tutorial25.pdf +} -- cgit v1.2.1-24-ge1ad