簡體   English   中英

矢量圖形的旋轉

[英]Rotation of vector graphics

我有一個用於在WinForms中繪制和編輯矢量圖形的應用程序

我有圖像,矩形,橢圓形,區域等,並且我知道如何通過鼠標移動來調整它們的大小。 但是我不知道如何通過鼠標移動來旋轉它們。

我將對象繪制到圖形中。

我已經嘗試過了,但是沒有用。

g.TranslateTransform((float)(this.Rectangle.X + this.Rectangle.Width / 2), (float)(this.Rectangle.Y + this.Rectangle.Height / 2));
g.RotateTransform(this.Rotation);
g.TranslateTransform(-(float)(this.Rectangle.X + this.Rectangle.Width / 2), -(float)(this.Rectangle.Y + this.Rectangle.Height / 2));
//g.TranslateTransform(-(float)(rect.X + rect.Width / 2), -(float)(rect.Y + rect.Height / 2));

g.DrawImage(img, rect);

g.ResetTransform();

這行不通,因為我不知道如何在新的(旋轉的)位置上找到對象的角,因此我無法調整它的大小...

您需要應用高中三角學。 如果您用Google搜索“ graphics.drawimage旋轉”,則會有很多文章

但是首先,您不應該轉換Graphics對象本身。 您只是想獲取圖像的新邊框。 去做這個:

  1. 取以原點為中心的圖像邊界框。 請記住,為DrawImage(Image,Point [])的好處,這被定義為三個點

     Point[] boundingBox = { new Point(-width /2, -height/2), new Point(width/2, -height/2), new Point(-width/2, height/2) }; 
  2. 使用Trig旋轉它。 通過以下功能輸入每個點:

     Point rotatePointAroundOrigin(Point point, float angleInDegrees) { float angle = angleInDegrees * Math.PI/180; // get angle in radians return new Point( point.X * Math.Cos(angle) - point.Y * Math.Sin(angle), point.X * Math.Sin(angle) + point.Y * Math.Cos(angle)); } 
  3. 將boundind框翻譯到它必須去的地方。 在其每個點上加上width / 2和height / 2,再加上您想要的任何額外量。

  4. 調用DrawImage(image, boundingBox)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM