Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 旋转矩形被移动(SVG-旋转矩阵)_Go_Svg_Rotation_Image Rotation - Fatal编程技术网

Go 旋转矩形被移动(SVG-旋转矩阵)

Go 旋转矩形被移动(SVG-旋转矩阵),go,svg,rotation,image-rotation,Go,Svg,Rotation,Image Rotation,我使用SVG处理一些公式。当我旋转一个矩形时,该矩形会移动 package main import ( "bytes" "fmt" "log" "math" "os" ) type Point struct { X, Y float64 } /* With SVG path the points are A B D C */ ty

我使用SVG处理一些公式。当我旋转一个矩形时,该矩形会移动

package main

import (
    "bytes"
    "fmt"
    "log"
    "math"
    "os"
)
type Point struct {
    X, Y float64
}

/* With SVG path the points are
    A B
    D C
*/
type Rect struct {
    A Point
    B Point
    C Point
    D Point
}

func (r *Rect) Rotate(a float64) {
    r.A.X = r.A.X * math.Cos(dToR(a)) - r.A.Y * math.Sin(dToR(a)) 
    r.A.Y = r.A.X * math.Sin(dToR(a)) + r.A.Y * math.Cos(dToR(a))

    r.B.X = r.B.X * math.Cos(dToR(a)) - r.B.Y * math.Sin(dToR(a)) 
    r.B.Y = r.B.X * math.Sin(dToR(a)) + r.B.Y * math.Cos(dToR(a))

    r.C.X = r.C.X * math.Cos(dToR(a)) - r.C.Y * math.Sin(dToR(a)) 
    r.C.Y = r.C.X * math.Sin(dToR(a)) + r.C.Y * math.Cos(dToR(a))

    r.D.X = r.D.X * math.Cos(dToR(a)) - r.D.Y * math.Sin(dToR(a)) 
    r.D.Y = r.D.X * math.Sin(dToR(a)) + r.D.Y * math.Cos(dToR(a))
}

// degree to radian
func dToR(deg float64) float64 {
    return deg * (math.Pi / 180.0)
}

// radian to degree
func rToD(rad float64) float64 {
    return rad * (180.0 / math.Pi)
}

func writeSvg(data []byte) error {
    f, err := os.OpenFile("test.svg", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
    if err != nil {
        return err
    }
    defer f.Close()

    _, err = f.Write(data)
    if err != nil {
        return err
    }

    return nil
}


func main() {

    var b bytes.Buffer
    b.WriteString("<svg width=\"600\" height=\"600\" xmlns=\"http://www.w3.org/2000/svg\">")
    b.WriteString("<g stroke=\"black\" stroke-width=\"2.5\" fill=\"none\">")
    b.WriteString("<path d=\"M")
    
    r := &Rect {
        Point{-200, -100},
        Point{200, -100},
        Point{200, 100},
        Point{-200, 100},
    }
    
    moveX := 300.0
    moveY := 300.0
    
    r.Rotate(30)
    
    b.WriteString(fmt.Sprintf("%.14f %.14f L", moveX+r.A.X, moveY+r.A.Y))           
    b.WriteString(fmt.Sprintf("%.14f %.14f ", moveX+r.B.X, moveY+r.B.Y))  
    b.WriteString(fmt.Sprintf("%.14f %.14f ", moveX+r.C.X, moveY+r.C.Y))   
    b.WriteString(fmt.Sprintf("%.14f %.14f Z", moveX+r.D.X, moveY+r.D.Y))

    b.WriteString("\"/>\n")   
    b.WriteString("</g></svg>")


    err := writeSvg(b.Bytes())
    if err != nil {
        log.Fatal(err)
    }
}
主程序包
进口(
“字节”
“fmt”
“日志”
“数学”
“操作系统”
)
类型点结构{
十、 Y浮动64
}
/*使用SVG路径,点是
A B
D C
*/
类型Rect struct{
一点
B点
C点
D点
}
func(r*Rect)旋转(浮点64){
r、 A.X=r.A.X*math.Cos(dToR(A))-r.A.Y*math.Sin(dToR(A))
r、 A.Y=r.A.X*math.Sin(dToR(A))+r.A.Y*math.Cos(dToR(A))
r、 B.X=r.B.X*math.Cos(dToR(a))-r.B.Y*math.Sin(dToR(a))
r、 B.Y=r.B.X*math.Sin(dToR(a))+r.B.Y*math.Cos(dToR(a))
r、 C.X=r.C.X*math.Cos(dToR(a))-r.C.Y*math.Sin(dToR(a))
r、 C.Y=r.C.X*math.Sin(dToR(a))+r.C.Y*math.Cos(dToR(a))
r、 D.X=r.D.X*math.Cos(dToR(a))-r.D.Y*math.Sin(dToR(a))
r、 D.Y=r.D.X*math.Sin(dToR(a))+r.D.Y*math.Cos(dToR(a))
}
//弧度
func dToR(deg浮点64)浮点64{
返回度*(数学Pi/180.0)
}
//弧度
func rToD(rad浮点64)浮点64{
返回rad*(180.0/math.Pi)
}
func writeSvg(数据[]字节)错误{
f、 err:=os.OpenFile(“test.svg”,os.O|RDWR | os.O|u CREATE | os.O|u TRUNC,0755)
如果错误!=零{
返回错误
}
延迟f.关闭()
_,err=f.Write(数据)
如果错误!=零{
返回错误
}
归零
}
func main(){
VARBytes.Buffer
b、 写字符串(“”)
b、 写字符串(“”)
b、 写入字符串(“\n”)
b、 写字符串(“”)
错误:=writeSvg(b.Bytes())
如果错误!=零{
log.Fatal(错误)
}
}
结果如下所示(黑色矩形)

矩形是旋转的,但角度不是90度。。。这是一个简单且有很好文档记录的公式,但我找不到我的bug


(SVG中还有其他绘制和旋转对象的方法,但我使用的是公式。)

rotate
函数中,您错误地计算了新的
x
y
坐标。您根据
r.A.X
r.A.Y
的当前值计算
r.A.X
,然后根据
r.A.X
的新值计算
r.A.Y
,这会让您失望

您需要知道原始的
x
y
值,然后根据这些值计算新值

rad := dToR(a)

x, y := r.A.X, r.A.Y
r.A.X = x*math.Cos(rad) - y*math.Sin(rad)
r.A.Y = x*math.Sin(rad) + y*math.Cos(rad)

我认为你正在围绕一个点{0,0}旋转每个顶点并重新绘制形状。在SVG中,可以使用“变换”属性旋转形状