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

📄 vector.c

📁 用C++写的几何要素类
💻 C
字号:
//==================================================================// Copyright 2002, softSurfer (www.softsurfer.com)// This code may be freely used and modified for any purpose// providing that this copyright notice is included with it.// SoftSurfer makes no warranty for this code, and cannot be held// liable for any real or imagined damage resulting from it's use.// Users of this code must verify correctness for their application.//==================================================================#include "point.h"#include "vector.h"//==================================================================// Vector Class Methods//==================================================================//------------------------------------------------------------------//  Unary Ops//------------------------------------------------------------------// Unary minusVector Vector::operator-() {	Vector v;	v.x = -x; v.y = -y; v.z = -z;	v.dimn = dimn;	return v;}// Unary 2D perp operatorVector Vector::operator~() {	if (dimn != 2) err = Edim;   // and continue anyway	Vector v;	v.x = -y; v.y = x; v.z = z;	v.dimn = dimn;	return v;}//------------------------------------------------------------------//  Scalar Ops//------------------------------------------------------------------// Scalar multiplicationVector operator*( int c, Vector w ) {	Vector v;	v.x = c * w.x;	v.y = c * w.y;	v.z = c * w.z;	v.dimn = w.dim();	return v;}Vector operator*( double c, Vector w ) {	Vector v;	v.x = c * w.x;	v.y = c * w.y;	v.z = c * w.z;	v.dimn = w.dim();	return v;}Vector operator*( Vector w, int c ) {	Vector v;	v.x = c * w.x;	v.y = c * w.y;	v.z = c * w.z;	v.dimn = w.dim();	return v;}Vector operator*( Vector w, double c ) {	Vector v;	v.x = c * w.x;	v.y = c * w.y;	v.z = c * w.z;	v.dimn = w.dim();	return v;}// Scalar divisionVector operator/( Vector w, int c ) {	Vector v;	v.x = w.x / c;	v.y = w.y / c;	v.z = w.z / c;	v.dimn = w.dim();	return v;}Vector operator/( Vector w, double c ) {	Vector v;	v.x = w.x / c;	v.y = w.y / c;	v.z = w.z / c;	v.dimn = w.dim();	return v;}//------------------------------------------------------------------//  Arithmetic Ops//------------------------------------------------------------------Vector Vector::operator+( Vector w ) {	Vector v;	v.x = x + w.x;	v.y = y + w.y;	v.z = z + w.z;	v.dimn = max( dimn, w.dim());	return v;}Vector Vector::operator-( Vector w ) {	Vector v;	v.x = x - w.x;	v.y = y - w.y;	v.z = z - w.z;	v.dimn = max( dimn, w.dim());	return v;}//------------------------------------------------------------------//  Products//------------------------------------------------------------------// Inner Dot Productdouble Vector::operator*( Vector w ) {	return (x * w.x + y * w.y + z * w.z);}// 2D Exterior Perp Productdouble Vector::operator|( Vector w ) {	if (dimn != 2) err = Edim;    // and continue anyway	return (x * w.y - y * w.x);}// 3D Exterior Cross ProductVector Vector::operator^( Vector w ) {	Vector v;	v.x = y * w.z - z * w.y;	v.y = z * w.x - x * w.z;	v.z = x * w.y - y * w.x;	v.dimn = 3;	return v;}//------------------------------------------------------------------//  Shorthand Ops//------------------------------------------------------------------Vector& Vector::operator*=( double c ) {        // vector scalar mult	x *= c;	y *= c;	z *= c;	return *this;}Vector& Vector::operator/=( double c ) {        // vector scalar div	x /= c;	y /= c;	z /= c;	return *this;}Vector& Vector::operator+=( Vector w ) {        // vector increment	x += w.x;	y += w.y;	z += w.z;	dimn = max(dimn, w.dim());	return *this;}Vector& Vector::operator-=( Vector w ) {        // vector decrement	x -= w.x;	y -= w.y;	z -= w.z;	dimn = max(dimn, w.dim());	return *this;}Vector& Vector::operator^=( Vector w ) {        // 3D exterior cross product	double ox=x, oy=y, oz=z;	x = oy * w.z - oz * w.y;	y = oz * w.x - ox * w.z;	z = ox * w.y - oy * w.x;	dimn = 3;	return *this;}//------------------------------------------------------------------//  Special Operations//------------------------------------------------------------------void Vector::normalize() {                      // convert to unit length	double ln = sqrt( x*x + y*y + z*z );	if (ln == 0) return;                    // do nothing for nothing	x /= ln;	y /= ln;	z /= ln;}Vector sum( int n, int c[], Vector w[] ) {     // vector sum	int     maxd = 0;	Vector  v;	for (int i=0; i<n; i++) {		if (w[i].dim() > maxd)			maxd = w[i].dim();	}	v.dimn = maxd;	for (int i=0; i<n; i++) {		v.x += c[i] * w[i].x;		v.y += c[i] * w[i].y;		v.z += c[i] * w[i].z;	}	return v;}Vector sum( int n, double c[], Vector w[] ) {  // vector sum	int     maxd = 0;	Vector  v;	for (int i=0; i<n; i++) {		if (w[i].dim() > maxd)			maxd = w[i].dim();	}	v.dimn = maxd;	for (int i=0; i<n; i++) {		v.x += c[i] * w[i].x;		v.y += c[i] * w[i].y;		v.z += c[i] * w[i].z;	}	return v;}

⌨️ 快捷键说明

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