Sign in to follow this  
xod

Plugin to draw angles

Recommended Posts

xod    174

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);
            }
          }
        }

 

Share this post


Link to post
Share on other sites
Red ochre    1,382

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?

 

Share this post


Link to post
Share on other sites
toe_head2001    792

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
{
}

 

Share this post


Link to post
Share on other sites
MJW    637

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

Share this post


Link to post
Share on other sites
toe_head2001    792

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this