C# 代码:
using System;
namespace BBKingsyeCore
{
public struct BBKVector2d
{
public double x;
public double y;
public const double equalNum = 9.999999E-11d;
public static BBKVector2d Zero
{
get
{
return new BBKVector2d(0, 0);
}
}
public static BBKVector2d One
{
get
{
return new BBKVector2d(1, 1);
}
}
public static BBKVector2d Max
{
get
{
return new BBKVector2d(double.MaxValue, double.MaxValue);
}
}
public static BBKVector2d UnitX
{
get
{
return new BBKVector2d(1, 0);
}
}
public static BBKVector2d UnitY
{
get
{
return new BBKVector2d(0, 1);
}
}
public double Magnitude
{
get
{
return Math.Sqrt(x * x + y * y);
}
}
public double SqrMagnitude
{
get
{
return x * x + y * y;
}
}
public BBKVector2d(double x, double y)
{
this.x = x;
this.y = y;
}
public BBKVector2d(BBKVector2d vec2)
{
this.x = vec2.x;
this.y = vec2.y;
}
public BBKVector2d Normalized
{
get
{
BBKVector2d res = new BBKVector2d(this);
double num = res.Magnitude;
if (num <= 1e-05d)
{
res.x = 0;
res.y = 0;
}
else
{
res.x /= num;
res.y /= num;
}
return res;
}
}
public BBKVector2d Inversed
{
get
{
return new BBKVector2d(-x, -y);
}
}
public void Inverse()
{
x = -x;
y = -y;
}
public void Normalize()
{
double num = Magnitude;
if (num <= 1e-05d)
{
x = 0;
y = 0;
}
else
{
x /= num;
y /= num;
}
}
public void Set(double _x, double _y)
{
x = _x;
y = _y;
}
public void Add(BBKVector2d a)
{
x += a.x;
y += a.y;
}
public void Add(double _x, double _y)
{
x += _x;
y += _y;
}
public void Sub(BBKVector2d a)
{
x -= a.x;
y -= a.y;
}
public void Sub(double _x, double _y)
{
x -= _x;
y -= _y;
}
public void Mul(float _x, float _y)
{
x *= _x;
y *= _y;
}
public void Mul(BBKVector2d v)
{
x *= v.x;
y *= v.y;
}
public double Dot(BBKVector2d v)
{
return x * v.x + y * v.y;
}
public void MoveForward(double angle, double speed)
{
x += speed * Math.Cos(angle);
y += speed * Math.Sin(angle);
}
public double Distance(BBKVector2d b)
{
return Math.Sqrt(DistanceSquared(b));
}
public double DistanceSquared(BBKVector2d b)
{
return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y);
}
public double Angle(BBKVector2d b)
{
return Math.Atan2(b.y - this.y, b.x - this.x) * 180f / Math.PI;
}
public override string ToString()
{
string dx = x.ToString("f4");
string dy = y.ToString("f4");
return string.Format("SimpleVector2({0}, {1})", dx, dy);
}
public static BBKVector2d Lerp(BBKVector2d from, BBKVector2d to, double t)
{
return new BBKVector2d(from.x + ((to.x - from.x) * t), from.y + ((to.y - from.y) * t));
}
public static double Distance(BBKVector2d a, BBKVector2d b)
{
return a.Distance(b);
}
public static double Angle(BBKVector2d from, BBKVector2d to)
{
return from.Angle(to);
}
public static BBKVector2d operator +(BBKVector2d a, BBKVector2d b)
{
return new BBKVector2d(a.x + b.x, a.y + b.y);
}
public static BBKVector2d operator +(BBKVector2d a, double[] b)
{
return new BBKVector2d(a.x + b[0], a.y + b[1]);
}
public static BBKVector2d operator -(BBKVector2d a, BBKVector2d b)
{
return new BBKVector2d(a.x - b.x, a.y - b.y);
}
public static BBKVector2d operator -(BBKVector2d a, double[] b)
{
return new BBKVector2d(a.x - b[0], a.y - b[1]);
}
public static BBKVector2d operator *(BBKVector2d a, int b)
{
return new BBKVector2d(a.x * b, a.y * b);
}
public static BBKVector2d operator *(BBKVector2d a, double b)
{
return new BBKVector2d(a.x * b, a.y * b);
}
public static BBKVector2d operator /(BBKVector2d a, int b)
{
return new BBKVector2d(a.x / b, a.y / b);
}
public static BBKVector2d operator /(BBKVector2d a, double b)
{
return new BBKVector2d(a.x / b, a.y / b);
}
public static bool operator ==(BBKVector2d lhs, BBKVector2d rhs)
{
return ((lhs - rhs).SqrMagnitude < equalNum);
}
public static bool operator !=(BBKVector2d lhs, BBKVector2d rhs)
{
return ((lhs - rhs).SqrMagnitude >= equalNum);
}
public override bool Equals(object obj)
{
return obj == null ? false : (x == ((BBKVector2d)obj).x && y == ((BBKVector2d)obj).y);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
public class Program
{
public static void Main()
{
BBKVector2d[] battlePoint = new BBKVector2d[3];
battlePoint[1] = new BBKVector2d(1,2);
battlePoint[2] = new BBKVector2d(3,6);
Console.WriteLine("比较对象 o1 + o2 :" + (battlePoint[1] + battlePoint[2]));
Console.WriteLine("比较对象 o1 - o2 :" + (battlePoint[1] - battlePoint[2]));
Console.WriteLine("比较对象 o1 * int :" + (battlePoint[1] * 3));
Console.WriteLine("比较对象 o1 == o2 :" + (battlePoint[1] == battlePoint[2]));
}
}
}
JavaScript 代码:
Bridge.assembly("Demo", function ($asm, globals) {
"use strict";
Bridge.define("BBKingsyeCore.BBKVector2d", {
$kind: "struct",
statics: {
fields: {
equalNum: 0
},
props: {
Zero: {
get: function () {
return new BBKingsyeCore.BBKVector2d.$ctor2(0, 0);
}
},
One: {
get: function () {
return new BBKingsyeCore.BBKVector2d.$ctor2(1, 1);
}
},
Max: {
get: function () {
return new BBKingsyeCore.BBKVector2d.$ctor2(System.Double.max, System.Double.max);
}
},
UnitX: {
get: function () {
return new BBKingsyeCore.BBKVector2d.$ctor2(1, 0);
}
},
UnitY: {
get: function () {
return new BBKingsyeCore.BBKVector2d.$ctor2(0, 1);
}
}
},
ctors: {
init: function () {
this.equalNum = 9.999999E-11;
}
},
methods: {
Lerp: function (from, to, t) {
return new BBKingsyeCore.BBKVector2d.$ctor2(from.x + ((to.x - from.x) * t), from.y + ((to.y - from.y) * t));
},
Distance: function (a, b) {
return a.Distance(b.$clone());
},
Angle: function (from, to) {
return from.Angle(to.$clone());
},
op_Addition: function (a, b) {
return new BBKingsyeCore.BBKVector2d.$ctor2(a.x + b.x, a.y + b.y);
},
op_Addition$1: function (a, b) {
return new BBKingsyeCore.BBKVector2d.$ctor2(a.x + b[System.Array.index(0, b)], a.y + b[System.Array.index(1, b)]);
},
op_Subtraction: function (a, b) {
return new BBKingsyeCore.BBKVector2d.$ctor2(a.x - b.x, a.y - b.y);
},
op_Subtraction$1: function (a, b) {
return new BBKingsyeCore.BBKVector2d.$ctor2(a.x - b[System.Array.index(0, b)], a.y - b[System.Array.index(1, b)]);
},
op_Multiply$1: function (a, b) {
return new BBKingsyeCore.BBKVector2d.$ctor2(a.x * b, a.y * b);
},
op_Multiply: function (a, b) {
return new BBKingsyeCore.BBKVector2d.$ctor2(a.x * b, a.y * b);
},
op_Division$1: function (a, b) {
return new BBKingsyeCore.BBKVector2d.$ctor2(a.x / b, a.y / b);
},
op_Division: function (a, b) {
return new BBKingsyeCore.BBKVector2d.$ctor2(a.x / b, a.y / b);
},
op_Equality: function (lhs, rhs) {
return ((BBKingsyeCore.BBKVector2d.op_Subtraction(lhs.$clone(), rhs.$clone())).SqrMagnitude < BBKingsyeCore.BBKVector2d.equalNum);
},
op_Inequality: function (lhs, rhs) {
return ((BBKingsyeCore.BBKVector2d.op_Subtraction(lhs.$clone(), rhs.$clone())).SqrMagnitude >= BBKingsyeCore.BBKVector2d.equalNum);
},
getDefaultValue: function () { return new BBKingsyeCore.BBKVector2d(); }
}
},
fields: {
x: 0,
y: 0
},
props: {
Magnitude: {
get: function () {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
},
SqrMagnitude: {
get: function () {
return this.x * this.x + this.y * this.y;
}
},
Normalized: {
get: function () {
var res = new BBKingsyeCore.BBKVector2d.$ctor1(this);
var num = res.Magnitude;
if (num <= 1E-05) {
res.x = 0;
res.y = 0;
} else {
res.x /= num;
res.y /= num;
}
return res.$clone();
}
},
Inversed: {
get: function () {
return new BBKingsyeCore.BBKVector2d.$ctor2(-this.x, -this.y);
}
}
},
ctors: {
$ctor2: function (x, y) {
this.$initialize();
this.x = x;
this.y = y;
},
$ctor1: function (vec2) {
this.$initialize();
this.x = vec2.x;
this.y = vec2.y;
},
ctor: function () {
this.$initialize();
}
},
methods: {
Inverse: function () {
this.x = -this.x;
this.y = -this.y;
},
Normalize: function () {
var num = this.Magnitude;
if (num <= 1E-05) {
this.x = 0;
this.y = 0;
} else {
this.x /= num;
this.y /= num;
}
},
Set: function (_x, _y) {
this.x = _x;
this.y = _y;
},
Add: function (a) {
this.x += a.x;
this.y += a.y;
},
Add$1: function (_x, _y) {
this.x += _x;
this.y += _y;
},
Sub: function (a) {
this.x -= a.x;
this.y -= a.y;
},
Sub$1: function (_x, _y) {
this.x -= _x;
this.y -= _y;
},
Mul$1: function (_x, _y) {
this.x *= _x;
this.y *= _y;
},
Mul: function (v) {
this.x *= v.x;
this.y *= v.y;
},
Dot: function (v) {
return this.x * v.x + this.y * v.y;
},
MoveForward: function (angle, speed) {
this.x += speed * Math.cos(angle);
this.y += speed * Math.sin(angle);
},
Distance: function (b) {
return Math.sqrt(this.DistanceSquared(b.$clone()));
},
DistanceSquared: function (b) {
return (this.x - b.x) * (this.x - b.x) + (this.y - b.y) * (this.y - b.y);
},
Angle: function (b) {
return Math.atan2(b.y - this.y, b.x - this.x) * 180.0 / Math.PI;
},
toString: function () {
var dx = System.Double.format(this.x, "f4");
var dy = System.Double.format(this.y, "f4");
return System.String.format("SimpleVector2({0}, {1})", dx, dy);
},
equals: function (obj) {
return obj == null ? false : (this.x === System.Nullable.getValue(Bridge.cast(Bridge.unbox(obj), BBKingsyeCore.BBKVector2d)).x && this.y === System.Nullable.getValue(Bridge.cast(Bridge.unbox(obj), BBKingsyeCore.BBKVector2d)).y);
},
getHashCode: function () {
return Bridge.getHashCode(this);
},
$clone: function (to) {
var s = to || new BBKingsyeCore.BBKVector2d();
s.x = this.x;
s.y = this.y;
return s;
}
}
});
Bridge.define("BBKingsyeCore.Program", {
main: function Main () {
var battlePoint = System.Array.init(3, function (){
return new BBKingsyeCore.BBKVector2d();
}, BBKingsyeCore.BBKVector2d);
battlePoint[System.Array.index(1, battlePoint)] = new BBKingsyeCore.BBKVector2d.$ctor2(1, 2);
battlePoint[System.Array.index(2, battlePoint)] = new BBKingsyeCore.BBKVector2d.$ctor2(3, 6);
System.Console.WriteLine("比较对象 o1 + o2 :" + (BBKingsyeCore.BBKVector2d.op_Addition(battlePoint[System.Array.index(1, battlePoint)].$clone(), battlePoint[System.Array.index(2, battlePoint)].$clone())));
System.Console.WriteLine("比较对象 o1 - o2 :" + (BBKingsyeCore.BBKVector2d.op_Subtraction(battlePoint[System.Array.index(1, battlePoint)].$clone(), battlePoint[System.Array.index(2, battlePoint)].$clone())));
System.Console.WriteLine("比较对象 o1 * int :" + (BBKingsyeCore.BBKVector2d.op_Multiply$1(battlePoint[System.Array.index(1, battlePoint)].$clone(), 3)));
System.Console.WriteLine("比较对象 o1 == o2 :" + System.Boolean.toString((BBKingsyeCore.BBKVector2d.op_Equality(battlePoint[System.Array.index(1, battlePoint)].$clone(), battlePoint[System.Array.index(2, battlePoint)].$clone()))));
}
});
});
TypeScript 代码:
declare module BBKingsyeCore {
export interface BBKVector2d {
x: number;
y: number;
Magnitude: number;
SqrMagnitude: number;
Normalized: BBKingsyeCore.BBKVector2d;
Inversed: BBKingsyeCore.BBKVector2d;
Inverse(): void;
Normalize(): void;
Set(_x: number, _y: number): void;
Add(a: BBKingsyeCore.BBKVector2d): void;
Add$1(_x: number, _y: number): void;
Sub(a: BBKingsyeCore.BBKVector2d): void;
Sub$1(_x: number, _y: number): void;
Mul$1(_x: number, _y: number): void;
Mul(v: BBKingsyeCore.BBKVector2d): void;
Dot(v: BBKingsyeCore.BBKVector2d): number;
MoveForward(angle: number, speed: number): void;
Distance(b: BBKingsyeCore.BBKVector2d): number;
DistanceSquared(b: BBKingsyeCore.BBKVector2d): number;
Angle(b: BBKingsyeCore.BBKVector2d): number;
toString(): string;
equals(obj: any): boolean;
getHashCode(): number;
$clone(to: BBKingsyeCore.BBKVector2d): BBKingsyeCore.BBKVector2d;
}
export interface BBKVector2dFunc extends Function {
prototype: BBKVector2d;
$ctor2: {
new (x: number, y: number): BBKVector2d
};
$ctor1: {
new (vec2: BBKingsyeCore.BBKVector2d): BBKVector2d
};
new (): BBKVector2d;
ctor: {
new (): BBKVector2d
};
equalNum: number;
Zero: BBKingsyeCore.BBKVector2d;
One: BBKingsyeCore.BBKVector2d;
Max: BBKingsyeCore.BBKVector2d;
UnitX: BBKingsyeCore.BBKVector2d;
UnitY: BBKingsyeCore.BBKVector2d;
Lerp(from: BBKingsyeCore.BBKVector2d, to: BBKingsyeCore.BBKVector2d, t: number): BBKingsyeCore.BBKVector2d;
Distance(a: BBKingsyeCore.BBKVector2d, b: BBKingsyeCore.BBKVector2d): number;
Angle(from: BBKingsyeCore.BBKVector2d, to: BBKingsyeCore.BBKVector2d): number;
op_Addition(a: BBKingsyeCore.BBKVector2d, b: BBKingsyeCore.BBKVector2d): BBKingsyeCore.BBKVector2d;
op_Addition$1(a: BBKingsyeCore.BBKVector2d, b: number[]): BBKingsyeCore.BBKVector2d;
op_Subtraction(a: BBKingsyeCore.BBKVector2d, b: BBKingsyeCore.BBKVector2d): BBKingsyeCore.BBKVector2d;
op_Subtraction$1(a: BBKingsyeCore.BBKVector2d, b: number[]): BBKingsyeCore.BBKVector2d;
op_Multiply$1(a: BBKingsyeCore.BBKVector2d, b: number): BBKingsyeCore.BBKVector2d;
op_Multiply(a: BBKingsyeCore.BBKVector2d, b: number): BBKingsyeCore.BBKVector2d;
op_Division$1(a: BBKingsyeCore.BBKVector2d, b: number): BBKingsyeCore.BBKVector2d;
op_Division(a: BBKingsyeCore.BBKVector2d, b: number): BBKingsyeCore.BBKVector2d;
op_Equality(lhs: BBKingsyeCore.BBKVector2d, rhs: BBKingsyeCore.BBKVector2d): boolean;
op_Inequality(lhs: BBKingsyeCore.BBKVector2d, rhs: BBKingsyeCore.BBKVector2d): boolean;
}
var BBKVector2d: BBKVector2dFunc;
export interface Program {
}
export interface ProgramFunc extends Function {
prototype: Program;
new (): Program;
Main(): void;
}
var Program: ProgramFunc;
}
运行结果:
比较对象 o1 + o2 :SimpleVector2(4.0000, 8.0000)
比较对象 o1 - o2 :SimpleVector2(-2.0000, -4.0000)
比较对象 o1 * int :SimpleVector2(3.0000, 6.0000)
比较对象 o1 == o2 :False