1
0
Fork 0

[server] Upgrade LocationReference to double to avoid awkward integer math problems in negative space

This commit is contained in:
Starbeamrainbowlabs 2017-12-15 20:47:45 +00:00
parent 073e5709bc
commit 6d543937ae
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
10 changed files with 43 additions and 38 deletions

View File

@ -343,8 +343,8 @@ namespace Nibriboard.Client
List<ChunkReference> initialChunks = new List<ChunkReference>();
ChunkReference currentChunkRef = new ChunkReference(
CurrentPlane,
CurrentViewPort.X / CurrentPlane.ChunkSize,
CurrentViewPort.Y / CurrentPlane.ChunkSize
(int)Math.Floor(CurrentViewPort.X / CurrentPlane.ChunkSize),
(int)Math.Floor(CurrentViewPort.Y / CurrentPlane.ChunkSize)
);
while(CanSee(currentChunkRef))
{
@ -354,7 +354,7 @@ namespace Nibriboard.Client
currentChunkRef = currentChunkRef.Clone() as ChunkReference;
currentChunkRef.X++;
}
currentChunkRef.X = CurrentViewPort.X;
currentChunkRef.X = (int)Math.Floor(CurrentViewPort.X / CurrentPlane.ChunkSize);
currentChunkRef.Y++;
}

View File

@ -119,11 +119,9 @@ class Pencil
if(!this.pencilDown)
return; // Don't draw anything if the left mouse button isn't down
// The server only supports ints atm, so we have to round here :-(
// TODO: Lift this limit
var nextPoint = new Vector(
Math.floor((event.clientX / this.boardWindow.viewport.zoomLevel) + this.boardWindow.viewport.x),
Math.floor((event.clientY / this.boardWindow.viewport.zoomLevel) + this.boardWindow.viewport.y)
(event.clientX / this.boardWindow.viewport.zoomLevel) + this.boardWindow.viewport.x,
(event.clientY / this.boardWindow.viewport.zoomLevel) + this.boardWindow.viewport.y
);
this.unsentSegments.push(nextPoint);
this.currentLineSegments.push(nextPoint);

View File

@ -1,6 +1,7 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using Nibriboard.RippleSpace;
using SBRL.Utilities;
namespace Nibriboard
@ -9,6 +10,12 @@ namespace Nibriboard
{
public static void Main(string[] args)
{
Plane testPlane = new Plane(new PlaneInfo("test-plane", 512), ".");
LocationReference testReference = new LocationReference(testPlane, -300, -250);
Console.WriteLine(testReference);
Console.WriteLine(testReference.ContainingChunk);
return;
string packedRippleSpaceFile = "./default.ripplespace.zip";
for(int i = 0; i < args.Length; i++)

View File

@ -13,7 +13,7 @@ namespace Nibriboard.RippleSpace
/// and obtained (A <see cref="NibriboardServer.RippleSpace.LocationReference" />
/// is returned).
/// </remarks>
public class ChunkReference : Reference
public class ChunkReference : Reference<int>
{
public ChunkReference(Plane inPlane, int inX, int inY) : base(inPlane, inX, inY)
{

View File

@ -7,7 +7,7 @@ namespace Nibriboard.RippleSpace
/// <summary>
/// Represents a location in absolute plane-space.
/// </summary>
public class LocationReference : Reference
public class LocationReference : Reference<double>
{
/// <summary>
/// The chunk that this location reference fall inside.
@ -19,12 +19,12 @@ namespace Nibriboard.RippleSpace
return new ChunkReference(
Plane,
X / Plane.ChunkSize,
Y / Plane.ChunkSize
(int)Math.Floor(X / Plane.ChunkSize),
(int)Math.Floor(Y / Plane.ChunkSize)
);
}
}
public LocationReference(Plane inPlane, int inX, int inY) : base(inPlane, inX, inY)
public LocationReference(Plane inPlane, double inX, double inY) : base(inPlane, inX, inY)
{
}

View File

@ -8,7 +8,7 @@ namespace Nibriboard.RippleSpace
/// An abstract class representing a coordinate reference to a location.
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public abstract class Reference : ICloneable
public abstract class Reference<T> : ICloneable
{
public Plane Plane { get; set; }
@ -27,14 +27,14 @@ namespace Nibriboard.RippleSpace
/// The x position of this reference.
/// </summary>
[JsonProperty]
public int X { get; set; }
public T X { get; set; }
/// <summary>
/// The y position of this reference.
/// </summary>
[JsonProperty]
public int Y { get; set; }
public T Y { get; set; }
public Reference(Plane inPlane, int inX, int inY)
public Reference(Plane inPlane, T inX, T inY)
{
Plane = inPlane;
X = inX; Y = inY;
@ -43,8 +43,8 @@ namespace Nibriboard.RippleSpace
/// Creates a new blank <see cref="Nibriboard.RippleSpace.Reference" />.
/// Don't use this yourself! This is only for Newtonsoft.Json to use when deserialising references.
/// </summary>
public Reference()
{
public Reference() {
}

View File

@ -26,8 +26,8 @@ namespace SBRL.Utilities
{
result.Add(new ChunkReference(
plane,
currentLocation.X / plane.ChunkSize,
currentLocation.Y / plane.ChunkSize
(int)Math.Floor(currentLocation.X / plane.ChunkSize),
(int)Math.Floor(currentLocation.Y / plane.ChunkSize)
));
currentLocation.X += plane.ChunkSize;

View File

@ -16,12 +16,12 @@ namespace Nibriboard.Utilities
while(true)
{
float smallestArea = float.MaxValue;
double smallestArea = float.MaxValue;
int smallestAreaI = 1;
for(int i = 1; i < points.Count - 1; i++)
{
float nextArea = TriangleArea(points[i - 1], points[i], points[i + 1]);
double nextArea = TriangleArea(points[i - 1], points[i], points[i + 1]);
if(nextArea < smallestArea) {
smallestArea = nextArea;
smallestAreaI = i;
@ -39,7 +39,7 @@ namespace Nibriboard.Utilities
return points;
}
public static float TriangleArea(LocationReference a, LocationReference b, LocationReference c)
public static double TriangleArea(LocationReference a, LocationReference b, LocationReference c)
{
return Math.Abs(
(

View File

@ -25,20 +25,20 @@ namespace SBRL.Utilities
/// <summary>
/// The X coordinate of the rectangle.
/// </summary>
public int X { get; set; }
public double X { get; set; }
/// <summary>
/// The Ycoordinateof the rectangle.
/// </summary>
/// <value>The y.</value>
public int Y { get; set; }
public double Y { get; set; }
/// <summary>
/// The width of the rectangle.
/// </summary>
public int Width { get; set; }
public double Width { get; set; }
/// <summary>
/// The height of the rectangle.
/// </summary>
public int Height { get; set; }
public double Height { get; set; }
#endregion
#region Corners
@ -85,7 +85,7 @@ namespace SBRL.Utilities
/// The Y coordinate of the top of the rectangle.
/// </summary>
[JsonIgnore]
public int Top {
public double Top {
get {
return Y;
}
@ -97,7 +97,7 @@ namespace SBRL.Utilities
/// The Y coordinate of the bottom of the rectangle.
/// </summary>
[JsonIgnore]
public int Bottom {
public double Bottom {
get {
return Y + Height;
}
@ -109,7 +109,7 @@ namespace SBRL.Utilities
/// The X coordinate of the left side of the rectangle.
/// </summary>
[JsonIgnore]
public int Left {
public double Left {
get {
return X;
}
@ -121,7 +121,7 @@ namespace SBRL.Utilities
/// The X coordinate of the right side of the rectangle.
/// </summary>
[JsonIgnore]
public int Right {
public double Right {
get {
return X + Width;
}
@ -131,7 +131,7 @@ namespace SBRL.Utilities
}
#endregion
public Rectangle(int x, int y, int width, int height)
public Rectangle(double x, double y, double width, double height)
{
X = x;
Y = y;

View File

@ -3,7 +3,7 @@
namespace SBRL.Utilities
{
/// <summary>
/// Represents a single point in 2D space.
/// Represents a single podouble in 2D space.
/// May also be used to represent a direction with a magnitude.
/// </summary>
/// <version>v0.1</version>
@ -21,14 +21,14 @@ namespace SBRL.Utilities
/// <summary>
/// The X coordinate.
/// </summary>
public int X { get; set; }
public double X { get; set; }
/// <summary>
/// The Y coordinate.
/// </summary>
public int Y { get; set; }
public double Y { get; set; }
public Vector2(int x, int y)
public Vector2(double x, double y)
{
X = x;
Y = y;
@ -49,14 +49,14 @@ namespace SBRL.Utilities
Y - b.X
);
}
public Vector2 Divide(int b)
public Vector2 Divide(double b)
{
return new Vector2(
X / b,
Y / b
);
}
public Vector2 Multiply(int b)
public Vector2 Multiply(double b)
{
return new Vector2(
X * b,