天天看點

VectorDraw入門必備手冊(十四):如何圍繞某個點旋轉折線中的頂點?

    VectorDraw Developer Framework(VDF)是一個用于應用程式可視化的圖形引擎庫。有了VDF提供的功能,您可以輕松地建立、編輯、管理、輸出、輸入和列印2D和3D圖形檔案。   

    VectorDraw web library (javascript)不僅能打開CAD圖紙,而且能顯示任何支援HTML5标準平台上的通用矢量對象,如Windows,安卓,iOS和Linux。無需任何安裝,VectorDraw web library (javascript)就可以運作在任何支援canvas标簽和Javascript的主流浏覽器(Chrome, Firefox, Safari, Opera, Dolphin, Boat等等)中。這意味着可以用DXF,DWG,DGN,SKP(Google的Sketchup),VDML等多種格式在任何台式、平闆電腦,智能手機和便攜式筆記本上展現出你的業務。

問:

    如何(通過代碼)可以圍繞某個點旋轉折線中的某些頂點,保持其他頂點相同?    

答:

這個問題非常簡單,您可以嘗試以下代碼: 

    private void MyButton_Click(object sender, EventArgs e)
        {
            vdDocument doc = vdFramedControl1.BaseControl.ActiveDocument; doc.New();
            Vertexes vrts = new Vertexes();
            vrts.Add(1,1,0,0);vrts.Add(1,4,0,0);vrts.Add(4,4,0,0);vrts.Add(5,3,0,0);
            vdPolyline pl = new vdPolyline(doc, vrts);
            doc.Model.Entities.AddItem(pl);
            pl.Invalidate();
            //--------------- created the polyline ---------
           
            
            // rotate it for 45 degrees anti-clockwise around vertex[1]
            
            Vertexes orig_vert = new Vertexes(pl.VertexList);//get the vertex list of the polyline that will be changed
            gPoint pt1 = new gPoint(orig_vert[1] as gPoint);
       
            // Vertexes from Item 2 and above will change
            Vertexes keep = new Vertexes();
            keep.Add(new Vertex(orig_vert[0]));
            keep.Add(new Vertex(orig_vert[1]));

            double orig_angle = pt1.GetAngle( orig_vert[2] as gPoint); // new angle
            orig_angle += VectorDraw.Geometry.Globals.DegreesToRadians(45.0d);

            Matrix mat = new Matrix();
            mat.TranslateMatrix(-1.0d * pt1);
            mat.RotateZMatrix(orig_angle);
            mat.TranslateMatrix(pt1);
            mat.Transform(orig_vert); // this will produce the new vertexes

            for (int i = 0; i < 2; i++)
            {
                orig_vert[i] = keep[i]; // restore the vertexes that didn't changed
            }

            pl.VertexList = orig_vert;
            pl.Update();
            pl.Invalidate();

        }