map-dots/imaging/circles.go

89 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-08-11 14:32:44 +01:00
package imaging
2023-08-11 20:51:46 +01:00
import (
"image"
"math"
2023-08-11 20:51:46 +01:00
"git.fjla.uk/fred.boniface/map-dots/data"
"git.fjla.uk/fred.boniface/map-dots/log"
"github.com/fogleman/gg"
)
func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) image.Image {
log.Msg.Debug("Mapping locations to canvas")
2023-08-11 20:51:46 +01:00
dc := gg.NewContextForRGBA(img)
dc.SetRGB(1, 1, 1) // Set canvas background color (white in this case)
2023-08-11 22:18:29 +01:00
//circleRadius := 4 // Replace with your desired fixed radius
2023-08-11 20:51:46 +01:00
bounds := img.Bounds()
canvasWidth := bounds.Max.X - bounds.Min.X
canvasHeight := bounds.Max.Y - bounds.Min.Y
2023-08-11 22:18:29 +01:00
margin := 0.1
2023-08-11 20:51:46 +01:00
2023-08-11 22:18:29 +01:00
var minLat, maxLat, minLon, maxLon = math.MaxFloat64, -math.MaxFloat64, math.MaxFloat64, -math.MaxFloat64
for _, loc := range locations {
if loc.Latitude < minLat {
minLat = loc.Latitude
2023-08-11 20:51:46 +01:00
}
if loc.Latitude > maxLat {
maxLat = loc.Latitude
2023-08-11 20:51:46 +01:00
}
if loc.Longitude < minLon {
minLon = loc.Longitude
2023-08-11 20:51:46 +01:00
}
if loc.Longitude > maxLon {
maxLon = loc.Longitude
2023-08-11 20:51:46 +01:00
}
}
latRange := maxLat - minLat
lonRange := maxLon - minLon
2023-08-11 22:18:29 +01:00
minLat -= latRange * margin
maxLat += latRange * margin
minLon -= lonRange * margin
maxLon += lonRange * margin
2023-08-11 22:18:29 +01:00
minLatEqui := equirectangularProjection(minLat)
maxLatEqui := equirectangularProjection(maxLat)
latScale := float64(canvasHeight) / (maxLatEqui - minLatEqui)
longScale := float64(canvasWidth) / (maxLon - minLon)
2023-08-11 20:51:46 +01:00
2023-08-11 22:18:29 +01:00
for _, loc := range locations {
x := int((loc.Longitude - minLon) * longScale)
// Invert the Y-axis calculation
y := canvasHeight - int((equirectangularProjection(loc.Latitude)-minLatEqui)*latScale)
// Draw a dot (circle) at (x, y)
dc.DrawCircle(float64(x), float64(y), 2)
dc.SetRGBA(1, 1, 1, 0.3333333333) // Set dot color (black)
2023-08-11 20:51:46 +01:00
dc.Fill()
}
return img
2023-08-11 20:51:46 +01:00
}
/*
func convertCoordinatesToPixels(latitude, longitude float64, centerLat, centerLon, latRange, lonRange float64, canvasWidth, canvasHeight int) (int, int) {
// Calculate normalized latitude and longitude distances from the center
latDist := (latitude - centerLat) / (latRange * 0.5)
lonDist := (longitude - centerLon) / (lonRange * 0.5)
// Calculate the maximum distance from the center as a proportion of canvas size
maxDistance := math.Max(math.Abs(latDist), math.Abs(lonDist))
// Adjust the normalized distances to match the canvas size with margins
adjustedLatDist := latDist / maxDistance * 0.45
adjustedLonDist := lonDist / maxDistance * 0.45
// Calculate pixel positions
pixelX := int((adjustedLonDist + 0.5) * float64(canvasWidth))
pixelY := int((0.5 - adjustedLatDist) * float64(canvasHeight))
2023-08-11 20:51:46 +01:00
return pixelX, pixelY
}*/
2023-08-11 22:18:29 +01:00
func equirectangularProjection(lat float64) float64 {
return lat * (math.Pi / 180) // Convert degrees to radians
}