Skip to content

Commit

Permalink
Another minor bugfix in GetIntersectPoint(). (#568)
Browse files Browse the repository at this point in the history
And a minor code tidy.
  • Loading branch information
AngusJohnson committed Jul 17, 2023
1 parent 5f1699d commit 562ef2d
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CPP/Clipper2Lib/include/clipper2/clipper.core.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ namespace Clipper2Lib
if (det == 0.0) return false;
double t = ((ln1a.x - ln2a.x) * dy2 - (ln1a.y - ln2a.y) * dx2) / det;
if (t <= 0.0) ip = ln1a; // ?? check further (see also #568)
else if (t >= 1.0) ip = ln2a; // ?? check further
else if (t >= 1.0) ip = ln1b; // ?? check further
else
{
ip.x = static_cast<int64_t>(ln1a.x + t * dx1);
Expand Down
18 changes: 9 additions & 9 deletions CSharp/Clipper2Lib/Clipper.Core.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 16 July 2023 *
* Date : 17 July 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : Core structures and functions for the Clipper Library *
Expand Down Expand Up @@ -631,18 +631,18 @@ internal static long CheckCastInt64(double val)
double dx1 = (ln1b.X - ln1a.X);
double dy2 = (ln2b.Y - ln2a.Y);
double dx2 = (ln2b.X - ln2a.X);
double cp = dy1 * dx2 - dy2 * dx1;
if (cp == 0.0)
double det = dy1 * dx2 - dy2 * dx1;
if (det == 0.0)
{
ip = new Point64();
return false;
}
double qx = dx1 * ln1a.Y - dy1 * ln1a.X;
double qy = dx2 * ln2a.Y - dy2 * ln2a.X;
ip = new Point64(
CheckCastInt64((dx1 * qy - dx2 * qx) / cp),
CheckCastInt64((dy1 * qy - dy2 * qx) / cp));
return (ip.X != Invalid64 && ip.Y != Invalid64);

double t = ((ln1a.X - ln2a.X) * dy2 - (ln1a.Y - ln2a.Y) * dx2) / det;
if (t <= 0.0) ip = ln1a;
else if (t >= 1.0) ip = ln1b;
else ip = new Point64(ln1a.X + t * dx1, ln1a.Y + t * dy1);
return true;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
8 changes: 0 additions & 8 deletions CSharp/Clipper2Lib/Clipper2Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,4 @@
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup>
<None Remove="*.ico" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Clipper.Offset_GOLD.cs" />
</ItemGroup>

</Project>
79 changes: 79 additions & 0 deletions CSharp/USINGZ.TestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 24 January 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* License : http://www.boost.org/LICENSE_1_0.txt *
*******************************************************************************/

using System.Collections.Generic;
using System.Reflection;
using System.IO;
using System;
using System.Security.Cryptography;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using System.Diagnostics;

using Clipper2Lib;

public class Application
{
public class MyCallbacks
{
public void MyCallback64(Point64 bot1, Point64 top1,
Point64 bot2, Point64 top2, ref Point64 intersectPt)
{
intersectPt.Z = 1;
}

public void MyCallbackD(PointD bot1, PointD top1,
PointD bot2, PointD top2, ref PointD intersectPt)
{
intersectPt.z = 1;
}
}

public static void Main()
{
PathsD solution = new PathsD();
PathsD subject = new PathsD();
subject.Add(Clipper.MakePath(new double[] { 100, 50, 10, 79, 65, 2, 65, 98, 10, 21 }));

ClipperD clipperD = new ClipperD();
MyCallbacks cb = new MyCallbacks();
clipperD.ZCallback = cb.MyCallbackD;
clipperD.AddSubject(subject);
clipperD.Execute(ClipType.Union, FillRule.NonZero, solution);


Console.WriteLine(solution.ToString(0));

SvgWriter svg= new SvgWriter(FillRule.NonZero);
SvgUtils.AddSubject(svg, subject);
SvgUtils.AddSolution(svg, solution, false);

PathsD ellipses = new PathsD();
for (int i = 0; i < solution[0].Count; i++)
{
if (solution[0][i].z == 1)
ellipses.Add(Clipper.Ellipse(
new PointD(solution[0][i].x, solution[0][i].y), 4));
}
svg.AddClosedPaths(ellipses, 0x20FF0000, 0xFFFF0000, 1);
svg.SaveToFile("usingz.svg", 300, 300);
OpenFileWithDefaultApp("usingz.svg");
}

public static void OpenFileWithDefaultApp(string filename)
{
string path = Path.GetFullPath(filename);
if (!File.Exists(path)) return;
Process p = new Process()
{
StartInfo = new ProcessStartInfo(path) { UseShellExecute = true }
};
p.Start();
}

}
23 changes: 23 additions & 0 deletions CSharp/USINGZ.TestApp/UsingZTestApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>$(DefineConstants);USINGZ</DefineConstants>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\USINGZ\Clipper2LibZ.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Utils\SVG\Clipper.SVG.cs" Link="Clipper.SVG.cs" />
<Compile Include="..\Utils\SVG\Clipper.SVG.Utils.cs" Link="Clipper.SVG.Utils.cs" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions CSharp/USINGZ.TestApp/UsingZ_TestApp.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33205.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UsingZTestApp", "UsingZTestApp.csproj", "{34A93225-8048-4A5A-8741-BC619C13C7B0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Clipper2LibZ", "..\USINGZ\Clipper2LibZ.csproj", "{C50EB9B5-CE07-49DE-B147-941DBFB4FA4E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{34A93225-8048-4A5A-8741-BC619C13C7B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34A93225-8048-4A5A-8741-BC619C13C7B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34A93225-8048-4A5A-8741-BC619C13C7B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34A93225-8048-4A5A-8741-BC619C13C7B0}.Release|Any CPU.Build.0 = Release|Any CPU
{C50EB9B5-CE07-49DE-B147-941DBFB4FA4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C50EB9B5-CE07-49DE-B147-941DBFB4FA4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C50EB9B5-CE07-49DE-B147-941DBFB4FA4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C50EB9B5-CE07-49DE-B147-941DBFB4FA4E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0E1221D6-32D4-4974-9205-DE97EC5F4937}
EndGlobalSection
EndGlobal
8 changes: 8 additions & 0 deletions CSharp/USINGZ/Clipper2LibZ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
<DefineConstants>TRACE;DEBUG;USINGZ;</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Clipper2Lib\Clipper.cs" Link="Clipper.cs" />
<Compile Include="..\Clipper2Lib\Clipper.Engine.cs" Link="Clipper.Engine.cs" />
<Compile Include="..\Clipper2Lib\Clipper.Offset.cs" Link="Clipper.Offset.cs" />
<Compile Include="..\Clipper2Lib\Clipper.Core.cs" Link="Clipper.Core.cs" />
<Compile Include="..\Clipper2Lib\Clipper.RectClip.cs" Link="Clipper.RectClip.cs" />
<Compile Include="..\Clipper2Lib\Clipper.Minkowski.cs" Link="Clipper.Minkowski.cs" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions Delphi/Clipper2Lib/Clipper.Core.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(*******************************************************************************
* Author : Angus Johnson *
* Date : 16 July 2023 *
* Date : 17 July 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : Core Clipper Library module *
Expand Down Expand Up @@ -1949,7 +1949,7 @@ function GetIntersectPoint(const ln1a, ln1b, ln2a, ln2b: TPoint64;
if not Result then Exit;
t := ((ln1a.x-ln2a.x) * dy2 - (ln1a.y-ln2a.y) * dx2) / cp;
if t <= 0.0 then ip := ln1a
else if t >= 1.0 then ip := ln2a;
else if t >= 1.0 then ip := ln1b;
ip.X := Trunc(ln1a.X + t * dx1);
ip.Y := Trunc(ln1a.Y + t * dy1);
end;
Expand Down
9 changes: 7 additions & 2 deletions Delphi/Clipper2Lib/Clipper.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(*******************************************************************************
* Author : Angus Johnson *
* Date : 16 July 2023 *
* Date : 17 July 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2023 *
* Purpose : This module provides a simple interface to the Clipper Library *
Expand Down Expand Up @@ -814,8 +814,13 @@ function PointInPolygon(const pt: TPoint64;

function DistanceSqrd(const pt1, pt2: TPoint64): double;
{$IFDEF INLINE} inline; {$ENDIF}
var
x1,y1,x2,y2: double;
begin
result := Sqr(double(pt1.X) - pt2.X) + Sqr(double(pt1.Y) - pt2.Y);
// nb: older versions of Delphi don't allow explicit typcasting
x1 := pt1.X; y1 := pt1.Y;
x2 := pt2.X; y2 := pt2.Y;
result := Sqr(x1 - x2) + Sqr(y1 - y2);
end;
//------------------------------------------------------------------------------

Expand Down

0 comments on commit 562ef2d

Please sign in to comment.