xod Posted January 3, 2017 Share Posted January 3, 2017 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); } } } Quote Link to comment Share on other sites More sharing options...
Red ochre Posted January 3, 2017 Share Posted January 3, 2017 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? Quote Red ochre Plugin pack.............. Diabolical Drawings ................Real Paintings Link to comment Share on other sites More sharing options...
toe_head2001 Posted January 3, 2017 Share Posted January 3, 2017 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 { } Quote My Gallery | My Plugin Pack Layman's Guide to CodeLab Link to comment Share on other sites More sharing options...
MJW Posted January 3, 2017 Share Posted January 3, 2017 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. | | |<------>| | | | | ->|--|<- | | 3 Quote Link to comment Share on other sites More sharing options...
toe_head2001 Posted January 4, 2017 Share Posted January 4, 2017 (edited) 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. Easy fix though. EDIT2: This was fixed in CodeLab v2.19. Edited April 16, 2017 by toe_head2001 1 Quote My Gallery | My Plugin Pack Layman's Guide to CodeLab Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.