# Type mismatch: cannot convert from float to int

**URL:** <https://discourse.processing.org/t/type-mismatch-cannot-convert-from-float-to-int/40434>\
**Category:** Coding Questions\
**Created:** [January 3, 2023, 10:12pm UTC](https://discourse.processing.org/t/type-mismatch-cannot-convert-from-float-to-int/40434 "2023-01-03T22:12:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [January 3, 2023, 10:12pm UTC](https://discourse.processing.org/t/type-mismatch-cannot-convert-from-float-to-int/40434/1 "2023-01-03T22:12:49Z")

</div>

Hi!

I am receiving the following errors when trying to implement a 3d height map of triangles.

“Type mismatch: cannot convert from float to int”

The function “triangle()” expects parameters like: “triangle(float, float, float, float, float, float)”

```auto
// Set the size of each triangle in the grid
int triangleSize = 10;

// Set the maximum height of the triangles in the grid
int maxHeight = 255;

// Set the color of the triangles in the grid
color triangleColor = color(255, 255, 255);

// Load the height map image into the sketch
PImage heightMap;

void setup() {
  // Set the size of the canvas
  size(800, 600, P3D);

  // Load the height map image into the sketch
  heightMap = loadImage("heightmap.jpg");
}

void draw() {
  // Clear the canvas to black
  background(0);

  // Iterate over the pixels in the height map
  for (int x = 0; x < heightMap.width; x++) {
    for (int y = 0; y < heightMap.height; y++) {
      // Get the color of the current pixel in the height map
      color pixelColor = heightMap.get(x, y);
      // Extract the red, green, and blue values from the pixel color
      int r = red(pixelColor);
      int g = green(pixelColor);
      int b = blue(pixelColor);
      // Calculate the height of the triangle based on the value of the pixel
      float height = maxHeight * (r + g + b) / (3 * 255.0);
      // Create a PVector for the position of the triangle
      PVector pos = new PVector((x - heightMap.width / 2) * triangleSize, (y - heightMap.height / 2) * triangleSize, height);
      // Set the fill color for the triangle
      fill(triangleColor);
      // Draw the triangle at the calculated position
      triangle(pos.x, pos.y, pos.z, pos.x + (float)triangleSize, pos.y, pos.z, pos.x + (float)triangleSize / 2, pos.y + (float)triangleSize, pos.z); // <-- added explicit type casts to float
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 3, 2023, 10:18pm UTC](https://discourse.processing.org/t/type-mismatch-cannot-convert-from-float-to-int/40434/2 "2023-01-03T22:18:49Z")

</div>

According to **triangle()**'s online reference it expects exactly 6 arguments:

> **[triangle() / Reference](https://processing.org/reference/triangle_.html)**
>
> A triangle is a plane created by connecting three points. The first two arguments specify the first point, the middle two arguments specify the second point, and the last two arguments specify the thi…

You’re passing 9 arguments; 3 more!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 3, 2023, 10:41pm UTC](https://discourse.processing.org/t/type-mismatch-cannot-convert-from-float-to-int/40434/3 "2023-01-03T22:41:01Z")

</div>

Other than point and line, triangle doesn’t know a 3D version as you attempted.

Here is a pyramid though (base with 3 corners):

see : [Find centre of pyramid and rotate from there, not the apex - #3 by Chrisir](https://discourse.processing.org/t/find-centre-of-pyramid-and-rotate-from-there-not-the-apex/40411/3)

**And with a Pyramid class**

- its base with 4 corners (iirc) EDITED
- The position you translate the pyramid to is exactly in the 3D center of the pyramid (like with box and sphere in P3D)

```auto
// Pyramid class

Pyramid pyramid;  

void setup() {
  size(768, 768, P3D);
  pyramid = new Pyramid( 200, height-221, -100 );
}

void draw() {
  background(0);
  lights(); 
  pyramid.display();
}

// ==============================================================================================

class Pyramid {

  float x, y, z; 
  PVector[] basePts = new PVector[4];

  //constr
  Pyramid( float x_, float y_, float z_) {
    x=x_;
    y=y_;
    z=z_;

    for (int i = 0; i < 4; i++ ) {
      float ang = TWO_PI * i / 4;
      basePts[i] = new PVector(
        cos(ang) * 90, 
        90/2.0, 
        sin(ang) * 90 );
      println(basePts[i]);
    } // for
  } // constr

  void display() {
    translate(x, y, z);
    // rotateX(radians(frameCount/8 % 360));
    // rotateY(radians(frameCount/2 % 360));

    fill(255);
    strokeWeight(3);
    stroke(123);

    //sides
    beginShape(TRIANGLES);
    for (int i = 0; i < 4; i++ ) {
      int i2 = (i+1) % 4;
      vertex(basePts[i].x, basePts[i].y, basePts[i].z);
      vertex(basePts[i2].x, basePts[i2].y, basePts[i2].z);
      vertex(0, -90/2.0, 0 );
    }
    endShape();//sides

    // show base
    fill(255, 0, 9); 
    beginShape(); //base
    for (int i = 0; i < 4; i++ ) {
      vertex(basePts[i].x, basePts[i].y, basePts[i].z);
    }
    endShape(CLOSE);
  } //method
  //
} // class
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 4, 2023, 6:01am UTC](https://discourse.processing.org/t/type-mismatch-cannot-convert-from-float-to-int/40434/4 "2023-01-04T06:01:18Z")

</div>

You can also use beginShape… vertex … in 3D
