Jump to content

Plugin to draw angles


xod

Recommended Posts

I try to make a plugin to draw angles.
But when I decrease the diameter to minimum and move the radius to zero the system crashes.
It is probably a math problem because if I give up to one of the arrows everythings works.
Some advices?

Thanks.

        #region UICode
        IntSliderControl Amount1 = 90; // [-180,180] Start
        DoubleSliderControl Amount2 = 0.5; // [0.1,1] Diameter
        #endregion
        
        void Render(Surface dst, Surface src, Rectangle rect)
        {
            Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
            float centerX = ((selection.Right - selection.Left) / 2)+selection.Left;
            float centerY = ((selection.Bottom - selection.Top) / 2)+selection.Top;
            float diameterU = (float)Amount2 * Math.Min(selection.Width - 4, selection.Height - 4);
            float radiusU = diameterU / 2f;
            float startU = (float)Amount1 - 90;
            float arc1 = radiusU*0.5f;
            float angle = (float)Amount1;
            
            dst.CopySurface(src, rect.Location, rect);
           
            using (RenderArgs ra = new RenderArgs(dst))
            {
                Graphics g = ra.Graphics;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.Clip = new Region(rect);
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                
                AdjustableArrowCap myArrow = new AdjustableArrowCap (3, 6);
                using (Pen gPen = new Pen(ColorBgra.Black))
                using (SolidBrush textBrush = new SolidBrush(Color.Black))
                using (Font textFont = new Font("Tahoma", 11, FontStyle.Regular))  
            {
            gPen.CustomEndCap = myArrow;
            float angleU = startU; 
            double radianU = Math.PI / 180 * angleU;
            float endX = (float)(radiusU * Math.Sin(radianU));
            float endXtxt = (float)((radiusU + 25) * Math.Sin(radianU));
            float endY = (float)(radiusU * Math.Cos(radianU)); 
            float endYtxt = (float)((radiusU + 25) * Math.Cos(radianU));
            g.DrawLine(gPen, centerX, centerY, centerX - endX, centerY - endY); 
            g.DrawString((angle).ToString(), textFont, textBrush, centerX - endXtxt, centerY - endYtxt, format);
            gPen.CustomStartCap = myArrow; //if I give up to this line everythings works
            g.DrawArc(gPen, centerX - arc1, centerY - arc1, arc1 * 2, arc1 * 2, 0, - angle);
            }
          }
        }

 

Link to comment
Share on other sites

I would imagine the GDI+ doesn't like trying to draw a line or an arc of zero length?
I've had similar problems before (eg.Cobweb).
Try enclosing the g.DrawLine and g.DrawArc in an 'if' statement, so it doesn't try to draw something below a minimum size?

 

 

Red ochre Plugin pack.............. Diabolical Drawings ................Real Paintings

 

PdnForumSig2.jpg

Link to comment
Share on other sites

Yeah, it's failing on DrawArc(), because there isn't enough space for your custom cap to be drawn. If you enclose DrawArc()in a try{} statement, you can watch as the caps slowly invert and then fail to draw as they become too close.

 

try
{
    g.DrawArc(gPen, centerX - arc1, centerY - arc1, arc1 * 2, arc1 * 2, 0, - angle);
}
catch
{
}

 

(September 25th, 2023)  Sorry about any broken images in my posts. I am aware of the issue.

bp-sig.png
My Gallery  |  My Plugin Pack

Layman's Guide to CodeLab

Link to comment
Share on other sites

Perhaps when the angle is less than about 20, you should reverse the direction of the arrows and move them outside the angle. That's what we did back many years ago when I had a drafting class.

 

  |        |
  |<------>|
  |        |
    
  |  |  
->|--|<-
  |  |

 

  • Upvote 3
Link to comment
Share on other sites

I don't like when paint.net crashes.

CodeLab ought to handle these type of exceptions, and display the exception message in the Errors pane.

 

EDIT: oh yeah, CodeLab already does that (duh).

CodeLab (and thus paint.net) is crashing, because the error tooltip in the editor is trying to get CompilerError information (error line, error column, ect) from that item in the Errors pane. Those types of exception errors don't have CompilerError information, so a null reference crash happens. >:O

Easy fix though. :D

 

EDIT2: This was fixed in CodeLab v2.19.

Edited by toe_head2001
  • Upvote 1

(September 25th, 2023)  Sorry about any broken images in my posts. I am aware of the issue.

bp-sig.png
My Gallery  |  My Plugin Pack

Layman's Guide to CodeLab

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...