### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Division by Zero bug in BGRABitmap  (Read 1701 times)

#### crazzzypeter

• Newbie
• Posts: 1
##### Division by Zero bug in BGRABitmap
« on: May 27, 2021, 06:23:55 pm »
Path drawing function can cause "Division by Zero" exception in Windows.
There is no error on Linux, but the drawing is not correct, the path is not closed. (see attached image).
The bug is in the ComputeWidePolylinePoints function in the BGRAPen.pas file. (see attached image).

I found a workaround that fixes the error, however this is a quick "dirty" hack:
Code: Pascal  [Select][+][-]
1.   //compute borders
2.   setlength(borders, length(pts)-1);
3.   for i := 0 to high(pts)-1 do
4.   begin
5.     dir := pts[i+1]-pts[i];
6.     len := VectLen(dir);
7.     if len <> 0 then// <- hack
8.       dir.Scale(1/len);
Attached to the post is an example that raises a "Division by Zero" exception on Windows.

I hope the author of BGRABitmap will take a look at this bug and the possibility of fixing it.

#### circular

• Hero Member
• Posts: 3717
##### Re: Division by Zero bug in BGRABitmap
« Reply #1 on: May 28, 2021, 06:42:25 pm »
Conscience is the debugger of the mind

#### josh

• Hero Member
• Posts: 817
##### Re: Division by Zero bug in BGRABitmap
« Reply #2 on: May 28, 2021, 09:13:33 pm »
Hi Circular,

I had a similar problem and created a function that helped in my situation,  whether its usefull for ur needs not sure.
Code: [Select]
`Function Special_Divide(ADividend ,ADivisor,ADefault:Double):Double;var ASign:Integer=1;begin  if Adividend<0 then ASign:=-1;  if abs(Adivisor)<=abs(ADefault) then Result:=ADefault*ASign  else    If (abs(ADividend)*abs(ADivisor))<abs(Adefault) then result:=ADefault*ASign    else result:=ADividend/ADivisor;end;      `
It is probably not the fastest/effecient way but it helped in my scenario and my needs; it may need adjusting
« Last Edit: May 28, 2021, 09:20:48 pm by josh »
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX..
Any posts made from 2015 will be based on Lazarus Trunk.

#### circular

• Hero Member
• Posts: 3717
##### Re: Division by Zero bug in BGRABitmap
« Reply #3 on: May 30, 2021, 11:30:18 am »
Thanks.

I suppose that in this case, it is a matter of skipping a point if the length is zero.
Conscience is the debugger of the mind