⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vec_coord.d2l

📁 转载其他网站的资料与大家分享
💻 D2L
字号:
/*******************************************************************************
*                                                                              *
*                              Vector Coord lib                                *
*                                    v0.9                                      *
*                                                                              *
* ---------------------------------------------------------------------------- *
* Copyright (C) 2002 Jan Onno Tuinenga (Scavenger) - tha_scavenger@hotmail.com *
* ---------------------------------------------------------------------------- *
*                                                                              *
* This program is free software; you can redistribute it and/or modify         *
* it under the terms of the GNU General Public License as published by         *
* the Free Software Foundation; either version 2 of the License, or            *
* (at your option) any later version.                                          *
*                                                                              *
* This program is distributed in the hope that it will be useful,              *
* but WITHOUT ANY WARRANTY; without even the implied warranty of               *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                *
* GNU General Public License for more details.                                 *
*                                                                              *
********************************************************************************/

/*******************************************************************************
* HISTORY:                                                                     *
* ---------------------------------------------------------------------------- *
* 0.9: - Fixed vector.rotate()                                                 *
* 0.8: - Added data property to coord.                                         *
* 0.7: - Length of a vector is now restored back to it's original length after *
*        rotate                                                                *
*      - Changed the comments for coord.move                                   *
*      - Fixed assignment of setlength in constructor (tnx 'Disturbed')        *
* 0.6: - Added coord.data                                                      *
*      - Fixed bug in coord.dist2                                              *
* 0.5: - First release                                                         *
*******************************************************************************/

// Constructor accepts x and y value or a vector

function vector()
{
	if(arguments.length == 2) {
		this.x = arguments[0];
		this.y = arguments[1];
	}
	else if (arguments.length == 1) {
		this.x = arguments[0].x;
		this.y = arguments[0].y;
	}
	else {
		this.x = 0;
		this.y = 0;
	}
	
	this.rotate = vector_rotate;
	this.normalize = vector_normalize;
	this.length = vector_length;
	this.setlength = vector_setlength;
	this.toString = vector_toString;
	this.set = vector_set;
	this.angle = vector_angle;
}


// returns false when failed (empty vector: 0,0)

function vector_rotate(degree)
{
	var l = this.length();
	if (!l)
		return false;
		
	this.normalize();
	
	rad = degree * (Math.PI/180);
	
	nx = this.x * Math.cos(rad) - this.y * Math.sin(rad);
	ny = this.x * Math.sin(rad) + this.y * Math.cos(rad);
	
	this.x = nx;
	this.y = ny;
	
	this.normalize();
	if (l != 1)
		this.setlength(l);
	return true;
}


// returns angle with vector v (float)

function vector_angle(v)
{
	rad_degree = 180/Math.PI;
	return Math.acos((this.x * v.x) + (this.y * v.y)) * rad_degree;
}


// returns length of vector (float)

function vector_length()
{
	return Math.sqrt((this.x * this.x) + (this.y * this.y));
}

// set the length of the vector to 'length'

function vector_setlength(length)
{
	this.normalize();
	this.x *= length;
	this.y *= length;
}


// returns false when failed -> empty vector: 0,0

function vector_normalize()
{
	l = this.length();
	if (!l) {
		return false;
	}
	
	this.x /= l;
	this.y /= l;
	
	return true;
}



// return the vector as string rouded to 3 digits

function vector_toString()
{
	str = "(";
	str += Math.round(this.x*1000)/1000;
	str += " , ";
	str += Math.round(this.y*1000)/1000;
	str += ") : ";
	str += Math.round(this.length()*1000)/1000;
	
	return str;
}

function vector_set(tx, ty)
{
	if (arguments.length == 1) {
		this.x = tx.x;
		this.y = tx.y;
	}
	else if (arguments.length == 2) {
		this.x = tx;
		this.y = ty;
	}
	else {
		print("ERROR: vector.set -> invalid param count");
		stop();
	}
}



/**************************************************
*                                                 *
*                  Coord Object                   *
*                                                 *
**************************************************/

function coord()
{
	this.data = null;
	
	if (arguments.length == 3) {
		this.x = arguments[0];
		this.y = arguments[1];
		this.data = arguments[2];
	}
	if (arguments.length == 2) {
		this.x = arguments[0];
		this.y = arguments[1];
	}
	else if(arguments.length == 1) {
		this.x = arguments[0].x;
		this.y = arguments[0].y;
	}
	else {
		this.x = 0;
		this.y = 0;
	}
	
	this.dist = coord_dist;
	this.dist2 = coord_dist2;
	this.dir = coord_dir;
	this.move = coord_move;
	this.set = coord_set;
	this.toString = coord_toString;
}


function coord_set(tx, ty, d)
{
	if (arguments.length == 1) {
		this.x = tx.x;
		this.y = tx.y;
		if (tx.data)
			this.data = tx.data;
		else
			this.data = null;
	}
	else if (arguments.length == 2) {
		this.x = tx;
		this.y = ty;
		this.data = null;
	}
	else if (arguments.length == 3) {
		this.x = tx;
		this.y = ty;
		this.data = d;
	}
	else {
		print("ERROR: coord.set -> invalid param count");
		stop();
	}
}

// returns distance to coord (int)

function coord_dist(tx, ty)
{
	if (arguments.length == 1) {
		vx = this.x - tx.x;
		vy = this.y - tx.y;
		return Math.round(Math.sqrt((vx*vx) + (vy*vy)));	
	}
	else if (arguments.length == 2) {
		vx = this.x - tx;
		vy = this.y - ty;
		return Math.round(Math.sqrt((vx*vx) + (vy*vy)));
	}
	else {
		print("ERROR: coord.dist -> invalid param count");
		stop();
	}
}

//return Math.floor((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }

function coord_dist2(tx, ty)
{
	if (arguments.length == 1) {
		vx = this.x - tx.x
		vy = this.y - tx.y;
		
		return Math.floor((vx * vx) + (vy * vy));
	}
	else if (arguments.length == 2) {
		vx = this.x - tx
		vy = this.y - ty;
		
		return Math.floor((vx * vx) + (vy * vy));
	}
	else {
		print("ERROR: coord.dist2 -> Invalid param count");
	}
}


// return normalized direction vector (vector)

function coord_dir(tx, ty)
{
	if (arguments.length == 1) {
		v = new vector(tx.x - this.x, tx.y - this.y);
		v.normalize();
		return v;
	}
	else if (arguments.length == 2) {
		v = new vector(tx - this.x, ty - this.y);
		v.normalize();
		return v;
	}
	else {
		print("ERROR: coord.dir -> invalid param count");
		stop();
	}
}


// moves coord by vector multiplied by scalar d or length of vector is 'd' = 0
// returns false when d = 0 and vec.length() = 0

function coord_move(vec, d)
{
	if (d) {
		this.x += Math.round(vec.x * d);
		this.y += Math.round(vec.y * d);
		return true;
	}
	else if (vec.length()) {
		this.x += Math.round(vec.x);
		this.y += Math.round(vec.y);
		return true;
	}
	
	return false;
}

function coord_toString()
{
	return "( " + this.x + " , " + this.y + " ) " + this.data;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -