Skip to content

Surface

1
surf(X, Y, Z);

example_surf_1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;
    auto [X, Y] = meshgrid(linspace(-5, +5, 40), linspace(-5, +5, 40));
    auto Z = transform(X, Y, [](double x, double y) {
        return 10 * 2 + pow(x, 2) - 10 * cos(2 * pi * x) + pow(y, 2) -
               10 * cos(2 * pi * y);
    });
    surf(X, Y, Z);

    show();
    return 0;
}

More examples

example_surf_2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;
    auto [X, Y] = meshgrid(iota(1, 0.5, 10), iota(1, 20));
    auto Z =
        transform(X, Y, [](double x, double y) { return sin(x) + cos(y); });
    surf(X, Y, Z);

    show();
    return 0;
}

example_surf_3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;
    auto [X, Y] = meshgrid(iota(1, 0.5, 10), iota(1, 20));
    auto Z =
        transform(X, Y, [](double x, double y) { return sin(x) + cos(y); });
    auto C = transform(X, Y, [](double x, double y) { return x * y; });
    surf(X, Y, Z, C);
    colorbar();

    show();
    return 0;
}

example_surf_4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;
    auto [X, Y] = meshgrid(iota(-5, 0.5, 5));
    auto Z = transform(
        X, Y, [](double x, double y) { return y * sin(x) - x * cos(y); });
    auto C = transform(X, Y, [](double x, double y) { return x * y; });
    surf(X, Y, Z)->face_alpha(0.5).edge_color("none");

    show();
    return 0;
}

example_surf_5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;
    auto [X, Y] = meshgrid(iota(1, .5, 10), iota(1, 20));
    auto Z =
        transform(X, Y, [](double x, double y) { return sin(x) + cos(y); });
    surf(X, Y, Z)->lighting(true);

    show();
    return 0;
}

example_surf_6

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;
    auto [X, Y] = meshgrid(iota(1, .5, 10), iota(1, 20));
    auto Z =
        transform(X, Y, [](double x, double y) { return sin(x) + cos(y); });
    surf(X, Y, Z)->edge_color("none").lighting(true);

    show();
    return 0;
}