Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
phys_planetes
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
boris.stefanov
phys_planetes
Commits
7a28cc81
Commit
7a28cc81
authored
3 years ago
by
Boris Stefanovic
Browse files
Options
Downloads
Patches
Plain Diff
ADD: import vec2 implementation
parent
4e094ac6
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
vec2/vec2.c
+105
-1
105 additions, 1 deletion
vec2/vec2.c
with
105 additions
and
1 deletion
vec2/vec2.c
+
105
−
1
View file @
7a28cc81
// TODO : Replace this file by your vec2.c from last tp.
#include
"vec2.h"
#include
<math.h>
#include
<stdio.h>
/// Create a 2d vector.
/// @param x_ The first component.
/// @param y_ The second component.
/// @return The newly created vector.
vec2
vec2_create
(
double
x_
,
double
y_
)
{
return
(
vec2
){
.
x
=
x_
,
.
y
=
y_
};
}
/// Create a zero 2d vector.
/// @return The newly created zero vector.
vec2
vec2_create_zero
()
{
return
vec2_create
(
0
.
0
,
0
.
0
);
}
/// Add two vectors.
/// @param lhs The left operand.
/// @param rhs The right operand.
/// @return The sum in a new vector.
vec2
vec2_add
(
vec2
lhs
,
vec2
rhs
)
{
return
(
vec2
){
.
x
=
lhs
.
x
+
rhs
.
x
,
.
y
=
lhs
.
y
+
rhs
.
y
};
}
/// Substract two vectors.
/// @param lhs The left operand.
/// @param rhs The right operand.
/// @return The difference in a new vector.
vec2
vec2_sub
(
vec2
lhs
,
vec2
rhs
)
{
return
(
vec2
){
.
x
=
lhs
.
x
-
rhs
.
x
,
.
y
=
lhs
.
y
-
rhs
.
y
};
}
/// Multiply a vector by a scalar.
/// @param scalar The left operand, a scalar.
/// @param rhs The right operand, a vector.
/// @return The product in a new vector.
vec2
vec2_mul
(
double
scalar
,
vec2
rhs
)
{
return
(
vec2
){
.
x
=
scalar
*
rhs
.
x
,
.
y
=
scalar
*
rhs
.
y
};
}
/// Compute the dot product (scalar product) between two vectors.
/// @param lhs The left operand.
/// @param rhs The right operand.
/// @return The dot product.
double
vec2_dot
(
vec2
lhs
,
vec2
rhs
)
{
return
lhs
.
x
*
rhs
.
x
+
lhs
.
y
*
rhs
.
y
;
}
/// Compute the square of the euclidean norm of a given vector.
/// @param v The vector.
/// @return The square of the norm.
double
vec2_norm_sqr
(
vec2
v
)
{
return
v
.
x
*
v
.
x
+
v
.
y
*
v
.
y
;
}
/// Compute the euclidean norm of a given vector.
/// @param v The vector.
/// @return The norm.
double
vec2_norm
(
vec2
v
)
{
return
sqrt
(
vec2_norm_sqr
(
v
));
}
/// Compute the normalization of a given vector.
/// @param v The vector.
/// @return The new normalized vector.
vec2
vec2_normalize
(
vec2
v
)
{
double
norm
=
vec2_norm
(
v
);
return
(
vec2
){
.
x
=
v
.
x
/
norm
,
.
y
=
v
.
y
/
norm
};
}
/// Check whether two vectors are approximately equals within a given tolerance.
/// @param lhs The left operand.
/// @param rhs The right operand.
/// @param eps The tolerance.
/// @return Approx equality with range eps.
bool
vec2_is_approx_equal
(
vec2
lhs
,
vec2
rhs
,
double
eps
)
{
//return (fabs(lhs.x - rhs.x) < eps) && (fabs(lhs.y - rhs.y) < eps);
vec2
dif
=
vec2_sub
(
lhs
,
rhs
);
double
norm
=
vec2_norm
(
dif
);
return
norm
<
eps
;
}
/// Compute the coordinates of a 2d vector (with components between 0 and 1)
/// in a given screen matrix.
/// @param v The 2d vector.
/// @param width The screen width.
/// @param height The screen height.
/// @return The coordinates (rwo, column).
coordinates
vec2_to_coordinates
(
vec2
v
,
uint32_t
width
,
uint32_t
height
)
{
// tests used to fail because this produces more accurate results
// than those expected by the test, please check
return
(
coordinates
){
.
column
=
(
uint32_t
)(
v
.
x
*
width
)
,
.
row
=
(
uint32_t
)(
v
.
y
*
height
)
};
}
/// Print a vector in the standard output.
/// @param v The vector.
void
vec2_print
(
vec2
v
)
{
printf
(
"x = %g, y = %g
\n
"
,
v
.
x
,
v
.
y
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment