| 1 |
/* MetaContour, version 0.1 -------------------------------------- */ |
| 2 |
/* Copyright(C) 2004, Brooks Moses */ |
| 3 |
/* */ |
| 4 |
/* This version of MetaContour is made available under the Gnu */ |
| 5 |
/* Public License; see metacontour_main.cc for details. */ |
| 6 |
/* */ |
| 7 |
/* This is a very pre-release version of MetaContour, distributed */ |
| 8 |
/* primarily as an example of a use of MetaPlot. It can be */ |
| 9 |
/* compiled with gcc using the line: */ |
| 10 |
/* */ |
| 11 |
/* g++ metacontour.cc cpoint.cc metacontour_main.cc */ |
| 12 |
/* */ |
| 13 |
/*-----------------------------------------------------------------*/ |
| 14 |
|
| 15 |
/* Class for 2D points with one data value each */ |
| 16 |
|
| 17 |
#include<sstream> |
| 18 |
#include"cpoint.h" |
| 19 |
using namespace std; |
| 20 |
|
| 21 |
|
| 22 |
//-----cpoint-------------------------------------------------- |
| 23 |
|
| 24 |
cpoint::cpoint() { |
| 25 |
x = 0; |
| 26 |
y = 0; |
| 27 |
z = 0; |
| 28 |
} |
| 29 |
|
| 30 |
cpoint::cpoint(double xx, double yy, double zz) { |
| 31 |
x = xx; |
| 32 |
y = yy; |
| 33 |
z = zz; |
| 34 |
} |
| 35 |
|
| 36 |
cpoint cpoint::interpolateto(cpoint x2, double z3) { |
| 37 |
double w = (z3-z)/(x2.z-z); |
| 38 |
if (w > 1) w=1; |
| 39 |
if (w < 0) w=0; |
| 40 |
double x3 = x + (x2.x-x)*w; |
| 41 |
double y3 = y + (x2.y-y)*w; |
| 42 |
return cpoint(x3,y3,z3); |
| 43 |
} |
| 44 |
|
| 45 |
cpoint cpoint::partwayto(cpoint x2, double w) { |
| 46 |
if (w > 1) w=1; |
| 47 |
if (w < 0) w=0; |
| 48 |
double x3 = x + (x2.x-x)*w; |
| 49 |
double y3 = y + (x2.y-y)*w; |
| 50 |
double z3 = z + (x2.z-z)*w; |
| 51 |
return cpoint(x3,y3,z3); |
| 52 |
} |
| 53 |
|
| 54 |
// Derivation: |
| 55 |
// F = ax + by + cxy + d x,y in [0,1] |
| 56 |
// Fx = a + by, Fy = b + cx |
| 57 |
// Thus: saddle point S is at (-b/c, -a/c) |
| 58 |
// Plugging this in, and solving, we get |
| 59 |
// F(S) = (f00*f11 - f01*f10) / (f00 + f11 - f01 - f10) |
| 60 |
// |
| 61 |
// Note: mapping x1 = (0,0), x2 = (1,0), x3 = (1,1), x4 = (0,1) |
| 62 |
// |
| 63 |
double cpoint::saddlepoint(cpoint x1, cpoint x2, cpoint x3, cpoint x4) { |
| 64 |
return (x1.z * x3.z - x2.z * x4.z) / (x1.z + x3.z - x2.z - x4.z); |
| 65 |
} |
| 66 |
|
| 67 |
string cpoint::metapoint() { |
| 68 |
ostringstream pointstring; |
| 69 |
pointstring.setf(ios_base::fixed,ios_base::floatfield); |
| 70 |
pointstring.precision(5); |
| 71 |
pointstring << '(' << x << ',' << y << ')'; |
| 72 |
return pointstring.str(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
//-----cparray------------------------------------------------- |
| 77 |
|
| 78 |
cparray::cparray(int x, int y) { |
| 79 |
if (x<1) x=1; |
| 80 |
if (y<1) y=1; |
| 81 |
i = x; |
| 82 |
j = y; |
| 83 |
a = new valarray<cpoint>(x*y); |
| 84 |
} |
| 85 |
|
| 86 |
cparray::~cparray() { |
| 87 |
delete a; |
| 88 |
} |
| 89 |
|
| 90 |
cpoint& cparray::operator()(int x, int y) |
| 91 |
{ |
| 92 |
return (*a)[x*j+y]; |
| 93 |
} |