initial commit
This commit is contained in:
79
geom/Axis.cs
Normal file
79
geom/Axis.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using QRCodeImageReader = ThoughtWorks.QRCode.Codec.Reader.QRCodeImageReader;
|
||||
namespace ThoughtWorks.QRCode.Geom
|
||||
{
|
||||
/// <summary> This class designed to move target point based on independent axis.
|
||||
/// It allows move target coodinate on rotated, scaled and gauche QR Code image
|
||||
/// </summary>
|
||||
public class Axis
|
||||
{
|
||||
|
||||
internal int sin, cos;
|
||||
internal int modulePitch;
|
||||
internal Point origin;
|
||||
|
||||
virtual public Point Origin
|
||||
{
|
||||
set
|
||||
{
|
||||
this.origin = value;
|
||||
}
|
||||
|
||||
}
|
||||
virtual public int ModulePitch
|
||||
{
|
||||
set
|
||||
{
|
||||
this.modulePitch = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Axis(int[] angle, int modulePitch)
|
||||
{
|
||||
this.sin = angle[0];
|
||||
this.cos = angle[1];
|
||||
this.modulePitch = modulePitch;
|
||||
this.origin = new Point();
|
||||
}
|
||||
|
||||
public virtual Point translate(Point offset)
|
||||
{
|
||||
int moveX = offset.X;
|
||||
int moveY = offset.Y;
|
||||
return this.translate(moveX, moveY);
|
||||
}
|
||||
|
||||
public virtual Point translate(Point origin, Point offset)
|
||||
{
|
||||
Origin = origin;
|
||||
int moveX = offset.X;
|
||||
int moveY = offset.Y;
|
||||
return this.translate(moveX, moveY);
|
||||
}
|
||||
|
||||
public virtual Point translate(Point origin, int moveX, int moveY)
|
||||
{
|
||||
Origin = origin;
|
||||
return this.translate(moveX, moveY);
|
||||
}
|
||||
|
||||
public virtual Point translate(Point origin, int modulePitch, int moveX, int moveY)
|
||||
{
|
||||
Origin = origin;
|
||||
this.modulePitch = modulePitch;
|
||||
return this.translate(moveX, moveY);
|
||||
}
|
||||
|
||||
public virtual Point translate(int moveX, int moveY)
|
||||
{
|
||||
long dp = QRCodeImageReader.DECIMAL_POINT;
|
||||
Point point = new Point();
|
||||
int dx = (moveX == 0)?0:(modulePitch * moveX) >> (int) dp;
|
||||
int dy = (moveY == 0)?0:(modulePitch * moveY) >> (int) dp;
|
||||
point.translate((dx * cos - dy * sin) >> (int) dp, (dx * sin + dy * cos) >> (int) dp);
|
||||
point.translate(origin.X, origin.Y);
|
||||
return point;
|
||||
}
|
||||
}
|
||||
}
|
||||
160
geom/Line.cs
Normal file
160
geom/Line.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
using System;
|
||||
using QRCodeUtility = ThoughtWorks.QRCode.Codec.Util.QRCodeUtility;
|
||||
|
||||
namespace ThoughtWorks.QRCode.Geom
|
||||
{
|
||||
public class Line
|
||||
{
|
||||
internal int x1, y1, x2, y2;
|
||||
|
||||
virtual public bool Horizontal
|
||||
{
|
||||
get
|
||||
{
|
||||
if (y1 == y2)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
virtual public bool Vertical
|
||||
{
|
||||
get
|
||||
{
|
||||
if (x1 == x2)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
virtual public Point Center
|
||||
{
|
||||
get
|
||||
{
|
||||
int x = (x1 + x2) / 2;
|
||||
int y = (y1 + y2) / 2;
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
}
|
||||
virtual public int Length
|
||||
{
|
||||
get
|
||||
{
|
||||
int x = System.Math.Abs(x2 - x1);
|
||||
int y = System.Math.Abs(y2 - y1);
|
||||
int r = QRCodeUtility.sqrt(x * x + y * y);
|
||||
return r;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Line()
|
||||
{
|
||||
x1 = y1 = x2 = y2 = 0;
|
||||
}
|
||||
|
||||
public Line(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
this.x1 = x1;
|
||||
this.y1 = y1;
|
||||
this.x2 = x2;
|
||||
this.y2 = y2;
|
||||
}
|
||||
public Line(Point p1, Point p2)
|
||||
{
|
||||
x1 = p1.X;
|
||||
y1 = p1.Y;
|
||||
x2 = p2.X;
|
||||
y2 = p2.Y;
|
||||
}
|
||||
public virtual Point getP1()
|
||||
{
|
||||
return new Point(x1, y1);
|
||||
}
|
||||
|
||||
public virtual Point getP2()
|
||||
{
|
||||
return new Point(x2, y2);
|
||||
}
|
||||
|
||||
public virtual void setLine(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
this.x1 = x1;
|
||||
this.y1 = y1;
|
||||
this.x2 = x2;
|
||||
this.y2 = y2;
|
||||
}
|
||||
public virtual void setP1(Point p1)
|
||||
{
|
||||
x1 = p1.X;
|
||||
y1 = p1.Y;
|
||||
}
|
||||
public virtual void setP1(int x1, int y1)
|
||||
{
|
||||
this.x1 = x1;
|
||||
this.y1 = y1;
|
||||
}
|
||||
public virtual void setP2(Point p2)
|
||||
{
|
||||
x2 = p2.X;
|
||||
y2 = p2.Y;
|
||||
}
|
||||
public virtual void setP2(int x2, int y2)
|
||||
{
|
||||
this.x2 = x2;
|
||||
this.y2 = y2;
|
||||
}
|
||||
|
||||
public virtual void translate(int dx, int dy)
|
||||
{
|
||||
this.x1 += dx;
|
||||
this.y1 += dy;
|
||||
this.x2 += dx;
|
||||
this.y2 += dy;
|
||||
}
|
||||
|
||||
//check if two lines are neighboring. allow only 1 dot difference
|
||||
public static bool isNeighbor(Line line1, Line line2)
|
||||
{
|
||||
if ((System.Math.Abs(line1.getP1().X - line2.getP1().X) < 2 && System.Math.Abs(line1.getP1().Y - line2.getP1().Y) < 2) && (System.Math.Abs(line1.getP2().X - line2.getP2().X) < 2 && System.Math.Abs(line1.getP2().Y - line2.getP2().Y) < 2))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool isCross(Line line1, Line line2)
|
||||
{
|
||||
if (line1.Horizontal && line2.Vertical)
|
||||
{
|
||||
if (line1.getP1().Y > line2.getP1().Y && line1.getP1().Y < line2.getP2().Y && line2.getP1().X > line1.getP1().X && line2.getP1().X < line1.getP2().X)
|
||||
return true;
|
||||
}
|
||||
else if (line1.Vertical && line2.Horizontal)
|
||||
{
|
||||
if (line1.getP1().X > line2.getP1().X && line1.getP1().X < line2.getP2().X && line2.getP1().Y > line1.getP1().Y && line2.getP1().Y < line1.getP2().Y)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public static Line getLongest(Line[] lines)
|
||||
{
|
||||
Line longestLine = new Line();
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
if (lines[i].Length > longestLine.Length)
|
||||
{
|
||||
longestLine = lines[i];
|
||||
}
|
||||
}
|
||||
return longestLine;
|
||||
}
|
||||
public override String ToString()
|
||||
{
|
||||
return "(" + System.Convert.ToString(x1) + "," + System.Convert.ToString(y1) + ")-(" + System.Convert.ToString(x2) + "," + System.Convert.ToString(y2) + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
93
geom/Point.cs
Normal file
93
geom/Point.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using QRCodeUtility = ThoughtWorks.QRCode.Codec.Util.QRCodeUtility;
|
||||
|
||||
namespace ThoughtWorks.QRCode.Geom
|
||||
{
|
||||
public class Point
|
||||
{
|
||||
public const int RIGHT = 1;
|
||||
public const int BOTTOM = 2;
|
||||
public const int LEFT = 4;
|
||||
public const int TOP = 8;
|
||||
|
||||
internal int x;
|
||||
internal int y;
|
||||
|
||||
|
||||
virtual public int X
|
||||
{
|
||||
get
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
this.x = value;
|
||||
}
|
||||
|
||||
}
|
||||
virtual public int Y
|
||||
{
|
||||
get
|
||||
{
|
||||
return y;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
this.y = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Point()
|
||||
{
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
public Point(int x, int y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public virtual void translate(int dx, int dy)
|
||||
{
|
||||
this.x += dx;
|
||||
this.y += dy;
|
||||
}
|
||||
|
||||
public virtual void set_Renamed(int x, int y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public override String ToString()
|
||||
{
|
||||
return "(" + System.Convert.ToString(x) + "," + System.Convert.ToString(y) + ")";
|
||||
}
|
||||
|
||||
public static Point getCenter(Point p1, Point p2)
|
||||
{
|
||||
return new Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
|
||||
}
|
||||
|
||||
public bool equals(Point compare)
|
||||
{
|
||||
if (x == compare.x && y == compare.y)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual int distanceOf(Point other)
|
||||
{
|
||||
int x2 = other.X;
|
||||
int y2 = other.Y;
|
||||
return QRCodeUtility.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
|
||||
}
|
||||
}
|
||||
}
|
||||
226
geom/SamplingGrid.cs
Normal file
226
geom/SamplingGrid.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using System;
|
||||
namespace ThoughtWorks.QRCode.Geom
|
||||
{
|
||||
|
||||
/// <summary> This class is used for sampling grid
|
||||
/// It allows one area to have a different size from another area
|
||||
/// </summary>
|
||||
public class SamplingGrid
|
||||
{
|
||||
virtual public int TotalWidth
|
||||
{
|
||||
get
|
||||
{
|
||||
int total = 0;
|
||||
for (int i = 0; i < grid.Length; i++)
|
||||
{
|
||||
total += grid[i][0].Width;
|
||||
if (i > 0)
|
||||
total -= 1;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
}
|
||||
virtual public int TotalHeight
|
||||
{
|
||||
get
|
||||
{
|
||||
int total = 0;
|
||||
for (int i = 0; i < grid[0].Length; i++)
|
||||
{
|
||||
total += grid[0][i].Height;
|
||||
if (i > 0)
|
||||
total -= 1;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary> A grid for a single area</summary>
|
||||
private class AreaGrid
|
||||
{
|
||||
private SamplingGrid enclosingInstance;
|
||||
private Line[] xLine;
|
||||
private Line[] yLine;
|
||||
|
||||
private void InitBlock(SamplingGrid enclosingInstance)
|
||||
{
|
||||
this.enclosingInstance = enclosingInstance;
|
||||
}
|
||||
|
||||
virtual public int Width
|
||||
{
|
||||
get
|
||||
{
|
||||
return (xLine.Length);
|
||||
}
|
||||
|
||||
}
|
||||
virtual public int Height
|
||||
{
|
||||
get
|
||||
{
|
||||
return (yLine.Length);
|
||||
}
|
||||
|
||||
}
|
||||
virtual public Line[] XLines
|
||||
{
|
||||
get
|
||||
{
|
||||
return xLine;
|
||||
}
|
||||
|
||||
}
|
||||
virtual public Line[] YLines
|
||||
{
|
||||
get
|
||||
{
|
||||
return yLine;
|
||||
}
|
||||
|
||||
}
|
||||
public SamplingGrid Enclosing_Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return enclosingInstance;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public AreaGrid(SamplingGrid enclosingInstance, int width, int height)
|
||||
{
|
||||
InitBlock(enclosingInstance);
|
||||
xLine = new Line[width];
|
||||
yLine = new Line[height];
|
||||
}
|
||||
|
||||
public virtual Line getXLine(int x)
|
||||
{
|
||||
return xLine[x];
|
||||
}
|
||||
|
||||
public virtual Line getYLine(int y)
|
||||
{
|
||||
return yLine[y];
|
||||
}
|
||||
|
||||
public virtual void setXLine(int x, Line line)
|
||||
{
|
||||
xLine[x] = line;
|
||||
}
|
||||
|
||||
public virtual void setYLine(int y, Line line)
|
||||
{
|
||||
yLine[y] = line;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private AreaGrid[][] grid;
|
||||
|
||||
public SamplingGrid(int sqrtNumArea)
|
||||
{
|
||||
grid = new AreaGrid[sqrtNumArea][];
|
||||
for (int i = 0; i < sqrtNumArea; i++)
|
||||
{
|
||||
grid[i] = new AreaGrid[sqrtNumArea];
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void initGrid(int ax, int ay, int width, int height)
|
||||
{
|
||||
grid[ax][ay] = new AreaGrid(this, width, height);
|
||||
}
|
||||
|
||||
public virtual void setXLine(int ax, int ay, int x, Line line)
|
||||
{
|
||||
grid[ax][ay].setXLine(x, line);
|
||||
}
|
||||
|
||||
public virtual void setYLine(int ax, int ay, int y, Line line)
|
||||
{
|
||||
grid[ax][ay].setYLine(y, line);
|
||||
}
|
||||
|
||||
public virtual Line getXLine(int ax, int ay, int x)
|
||||
{
|
||||
return (grid[ax][ay].getXLine(x));
|
||||
}
|
||||
|
||||
public virtual Line getYLine(int ax, int ay, int y)
|
||||
{
|
||||
return (grid[ax][ay].getYLine(y));
|
||||
}
|
||||
|
||||
public virtual Line[] getXLines(int ax, int ay)
|
||||
{
|
||||
return (grid[ax][ay].XLines);
|
||||
}
|
||||
|
||||
public virtual Line[] getYLines(int ax, int ay)
|
||||
{
|
||||
return (grid[ax][ay].YLines);
|
||||
}
|
||||
|
||||
public virtual int getWidth()
|
||||
{
|
||||
return (grid[0].Length);
|
||||
}
|
||||
|
||||
public virtual int getHeight()
|
||||
{
|
||||
return (grid.Length);
|
||||
}
|
||||
|
||||
public virtual int getWidth(int ax, int ay)
|
||||
{
|
||||
return (grid[ax][ay].Width);
|
||||
}
|
||||
|
||||
public virtual int getHeight(int ax, int ay)
|
||||
{
|
||||
return (grid[ax][ay].Height);
|
||||
}
|
||||
|
||||
|
||||
public virtual int getX(int ax, int x)
|
||||
{
|
||||
int total = x;
|
||||
for (int i = 0; i < ax; i++)
|
||||
{
|
||||
total += grid[i][0].Width - 1;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public virtual int getY(int ay, int y)
|
||||
{
|
||||
int total = y;
|
||||
for (int i = 0; i < ay; i++)
|
||||
{
|
||||
total += grid[0][i].Height - 1;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public virtual void adjust(Point adjust)
|
||||
{
|
||||
int dx = adjust.X, dy = adjust.Y;
|
||||
for (int ay = 0; ay < grid[0].Length; ay++)
|
||||
{
|
||||
for (int ax = 0; ax < grid.Length; ax++)
|
||||
{
|
||||
for (int i = 0; i < grid[ax][ay].XLines.Length; i++)
|
||||
grid[ax][ay].XLines[i].translate(dx, dy);
|
||||
for (int j = 0; j < grid[ax][ay].YLines.Length; j++)
|
||||
grid[ax][ay].YLines[j].translate(dx, dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user