1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| func TestRotating(t *testing.T) { f, _ := os.Open("C:/Users/Admin/Desktop/one.png") sImg, _ := png.Decode(f) r := sImg.Bounds().Max.Y / 4 tarImg := image.NewNRGBA(image.Rect(0 ,0 , 2 * r, 2 * r)) trX, trY := tarImg.Bounds().Max.X / 2, tarImg.Bounds().Max.Y / 2 for x := 0; x <= tarImg.Bounds().Max.X; x++ { for y := 0; y <= tarImg.Bounds().Max.Y; y++ { tarImg.SetNRGBA(x, y, color.NRGBA{255, 255, 255, 0}) if (x-trX)*(x-trX) + (y-trY)*(y-trY) <= r*r { cr, g, b, a := sImg.At(sImg.Bounds().Max.X/2-r+x, sImg.Bounds().Max.Y/2-r+y).RGBA() tarImg.SetNRGBA(x, y, color.NRGBA{*(*uint8)(unsafe.Pointer(&cr)), *(*uint8)(unsafe.Pointer(&g)), *(*uint8)(unsafe.Pointer(&b)), *(*uint8)(unsafe.Pointer(&a))}) } } } retImg := image.NewNRGBA(image.Rect(0 ,0 , 2 * r, 2 * r)) for x := 0; x <= retImg.Bounds().Max.X; x++ { for y := 0; y <= retImg.Bounds().Max.Y; y++ { if (x-trX)*(x-trX) + (y-trY)*(y-trY) <= r*r { tx, ty := AngleSwapPoint(float64(x), float64(y), float64(r), 45) cr, g, b, a := tarImg.At(int(tx), int(ty)).RGBA() retImg.SetNRGBA(x, y, color.NRGBA{*(*uint8)(unsafe.Pointer(&cr)), *(*uint8)(unsafe.Pointer(&g)), *(*uint8)(unsafe.Pointer(&b)), *(*uint8)(unsafe.Pointer(&a))}) } } } tf, _ := os.Create("C:/Users/Admin/Desktop/tar.png") png.Encode(tf, retImg) tar2, _ := os.Create("C:/Users/Admin/Desktop/tar2.png") png.Encode(tar2, tarImg) }
func AngleSwapPoint(x, y, r, angle float64) (tarX, tarY float64) { x -= r y = r - y sinVal := math.Sin(angle*(math.Pi/180)) cosVal := math.Cos(angle*(math.Pi/180)) tarX = x*cosVal + y*sinVal tarY = -x*sinVal + y*cosVal tarX += r tarY = r - tarY return }
|