1 前言
svg图片默认按照左上角旋转,改为按中心旋转,重新计算中心偏移量
2 代码
type Point struct { X float64 Y float64 } func GetOffsetXYAfterRotationForSVG(W, H, L, T, Angle float64) (ret Point) { AngleRad := Angle * math.Pi / 180 SinX := math.Sin(AngleRad) CosX := math.Cos(AngleRad) Z := math.Sqrt(math.Pow(W, 2)+math.Pow(H, 2)) / 2 AngleRadBeta := math.Atan(H / W) AngleBeta := AngleRadBeta * 180 / math.Pi fmt.Println("Angle,SinX,CosX,H,W,L,T,ZHalf,AngleBeta=", Angle, SinX, CosX, H, W, L, T, Z, AngleBeta) translate := Point{L, T} //开始计算,在以对角线一半为半径画圆,初始化角度为Anglebeta Angle = Angle + AngleBeta p0 := Point{Z * math.Cos(AngleRadBeta), Z * math.Sin(AngleRadBeta)} //ShowPoint(p0) AngleRad = Angle * math.Pi / 180 SinX = math.Sin(AngleRad) CosX = math.Cos(AngleRad) p1 := Point{Z * CosX, Z * SinX} //ShowPoint(p1) p1.Minus(p0) translate.Minus(p1) //ShowPoint(p1) fmt.Printf("p0=%+v,p1=%+v,p2=%+v", p0, p1, translate) fmt.Println() //保存精度 translate.Decimal(3) ret = translate return } func DecimalPrec(value float64, prec int) float64 { value, _ = strconv.ParseFloat(fmt.Sprintf("%."+strconv.Itoa(prec)+"f", value), 64) return value } func (p1 *Point) Add(p0 Point) { p1.X = p1.X + p0.X p1.Y = p1.Y + p0.Y return } func (p1 *Point) Minus(p0 Point) { p1.X = p1.X - p0.X p1.Y = p1.Y - p0.Y return } func (p1 *Point) Times(scale float64) { p1.X = p1.X * scale p1.Y = p1.Y * scale return } func (p1 *Point) Decimal(prec int) { p1.X = DecimalPrec(p1.X, prec) p1.Y = DecimalPrec(p1.Y, prec) return } func ShowPoint(p0 Point) { fmt.Printf("point x=%v,y=%v", p0.X, p0.Y) fmt.Println() }
3 输出
----start---- Angle,SinX,CosX,H,W,L,T,ZHalf,AngleBeta= 30 0.49999999999999994 0.8660254037844387 400 400 0 0 282.842712474619 45 p0={X:200.00000000000003 Y:200},p1={X:-126.7949192431123 Y:73.20508075688775},p2={X:126.7949192431123 Y:-73.20508075688775} x,y= 126.795 , -73.205 ----end----