1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
| /////////////////////////////////////////////////////////////////////////////////////////
//Нахождение площади параллелограмма, треугольника и объема параллелепипеда, тетраэдра
//по координатам вершин.
//Задается пользователем: координаты четырех точек и одна из задач
//а) найти площадь треугольника с вершинами в трех их заданных точек;
//б) найти площадь параллелограмма, построенного на векторах, с началом в одной из заданных
//точек и концами в двух других;
//в) найти объем тетраэдра с вершинами в заданных точках;
//г) найти объем параллелепипеда, построенного на векторах, с началом в одной из заданных
//точек и кон-цами в трех других.
//Выдается программой: полный ответ на поставленную задачу, например «объем параллелепипеда,
//построенного на векторах ВС и ВА равен 6,2 ед.3»
/////////////////////////////////////////////////////////////////////////////////////////
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
/////////////////////////////////////////////////////////////////////////////////////////
typedef std::string T_str;
/////////////////////////////////////////////////////////////////////////////////////////
struct T_3D_vect
{
//-----------------------------------------------------------------------------------
double x_;
double y_;
double z_;
//-----------------------------------------------------------------------------------
T_3D_vect()
:
x_(),
y_(),
z_()
{}
//-----------------------------------------------------------------------------------
T_3D_vect
(
double x,
double y,
double z
)
:
x_( x ),
y_( y ),
z_( z )
{}
//-----------------------------------------------------------------------------------
T_3D_vect operator- ( T_3D_vect const & v ) const
{
return T_3D_vect
(
x_ - v.x_,
y_ - v.y_,
z_ - v.z_
);
}
//-----------------------------------------------------------------------------------
static T_3D_vect from_point_to_point
(
T_3D_vect const & A,
T_3D_vect const & B
)
{
return B - A;
}
//-----------------------------------------------------------------------------------
T_3D_vect vector_product( T_3D_vect const & v ) const
{
return T_3D_vect
(
y_*v.z_ - z_*v.y_,
z_*v.x_ - x_*v.z_,
x_*v.y_ - y_*v.z_
);
}
//-----------------------------------------------------------------------------------
double operator* ( T_3D_vect const & v ) const
{
return x_*v.x_ + y_*v.y_ + z_*v.z_;
}
//-----------------------------------------------------------------------------------
double triple_product
(
T_3D_vect const & v,
T_3D_vect const & w
) const
{
return *this * v.vector_product( w );
}
//-----------------------------------------------------------------------------------
double abs() const
{
return sqrt( *this * *this );
}
//-----------------------------------------------------------------------------------
};
/////////////////////////////////////////////////////////////////////////////////////////
typedef T_3D_vect T_3D_point;
/////////////////////////////////////////////////////////////////////////////////////////
template< typename T >
void print_prompt_and_input_value_from_stream
(
T_str const & prompt,
T & val,
std::istream & istr = std::cin
)
{
std::cout << prompt;
std::cin >> val;
}
/////////////////////////////////////////////////////////////////////////////////////////
std::istream & operator>>
(
std::istream & istr,
T_3D_point & _3D_point
)
{
print_prompt_and_input_value_from_stream
(
"\tx = ",
_3D_point.x_,
istr
);
print_prompt_and_input_value_from_stream
(
"\ty = ",
_3D_point.y_,
istr
);
print_prompt_and_input_value_from_stream
(
"\tz = ",
_3D_point.z_,
istr
);
return istr;
}
/////////////////////////////////////////////////////////////////////////////////////////
typedef T_3D_vect T_3D_point;
/////////////////////////////////////////////////////////////////////////////////////////
double get_area_of_parallelogram_constructed_on_vectors_that_start_at_point_and_ends_at_points
(
T_3D_point const & A,
T_3D_point const & B,
T_3D_point const & C
)
{
T_3D_vect AB = T_3D_vect::from_point_to_point( A, B );
T_3D_vect AC = T_3D_vect::from_point_to_point( A, C );
return AB.vector_product( AC ).abs();
}
/////////////////////////////////////////////////////////////////////////////////////////
double get_area_of_triangle_with_vertices_at_points
(
T_3D_point const & A,
T_3D_point const & B,
T_3D_point const & C
)
{
return get_area_of_parallelogram_constructed_on_vectors_that_start_at_point_and_ends_at_points( A, B, C )
/ 2;
}
/////////////////////////////////////////////////////////////////////////////////////////
double get_volume_parallelepiped_constructed_on_vectors_with_starting_point_and_ends_at_points
(
T_3D_point const & A,
T_3D_point const & B,
T_3D_point const & C,
T_3D_point const & D
)
{
T_3D_vect AB = T_3D_vect::from_point_to_point( A, B );
T_3D_vect AC = T_3D_vect::from_point_to_point( A, C );
T_3D_vect AD = T_3D_vect::from_point_to_point( A, D );
return abs (
AB.triple_product( AC, AD )
);
}
/////////////////////////////////////////////////////////////////////////////////////////
double get_volume_of_tetrahedron_with_vertices_at_points
(
T_3D_point const & A,
T_3D_point const & B,
T_3D_point const & C,
T_3D_point const & D
)
{
return get_volume_parallelepiped_constructed_on_vectors_with_starting_point_and_ends_at_points( A, B, C, D )
/ 6;
}
/////////////////////////////////////////////////////////////////////////////////////////
int main()
{
std::locale::global(std::locale(""));
const int POINTS_TOTAL = 4;
std::cout << "Введите "
<< POINTS_TOTAL
<< " трехмерные точки:"
<< std::endl;
T_3D_point A;
T_3D_point B;
T_3D_point C;
T_3D_point D;
print_prompt_and_input_value_from_stream( "\n\nA:", A );
print_prompt_and_input_value_from_stream( "\n\nB:", B );
print_prompt_and_input_value_from_stream( "\n\nC:", C );
print_prompt_and_input_value_from_stream( "\n\nD:", D );
std::cout
<< std::endl
<< "Площадь треугольника с вершинами в точках A, B и C равна: "
<< get_area_of_triangle_with_vertices_at_points( A, B, C )
<< std::endl
<< std::endl
<< "Площадь параллелограмма, построенного на векторах, с началом в точке A"
<< std::endl
<< "и концами в точках B и C равна: "
<< get_area_of_parallelogram_constructed_on_vectors_that_start_at_point_and_ends_at_points( A, B, C )
<< std::endl
<< std::endl
<< "Объем тетраэдра с вершинами в точках A, B C и D равен: "
<< get_volume_of_tetrahedron_with_vertices_at_points( A, B, C, D )
<< std::endl
<< std::endl
<< "Объем параллелепипеда, построенного на векторах, с началом в точке A"
<< std::endl
<< "и концами в точках B, C и D равен: "
<< get_volume_parallelepiped_constructed_on_vectors_with_starting_point_and_ends_at_points( A, B, C, D )
<< std::endl;
system("pause");
} |