# Getting 2d coordinates from 3d point?

**URL:** https://discourse.vtk.org/t/getting-2d-coordinates-from-3d-point/3425
**Category:** Support
**Created:** [May 31, 2020, 8:17pm UTC](https://discourse.vtk.org/t/getting-2d-coordinates-from-3d-point/3425 "2020-05-31T20:17:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mdaley](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mdaley/32/1872_2.png) [@mdaley](https://discourse.vtk.org/u/mdaley)
#### Post date: [May 31, 2020, 8:17pm UTC](https://discourse.vtk.org/t/getting-2d-coordinates-from-3d-point/3425/1 "2020-05-31T20:17:49Z")

</div>

Hello, I have some additional points that I’ve added into my own implementation of the vtkChartXYZ. Actually, they are the mid points of all of the vertices of the 3d box that bounds the chart. What I would like to be able to do is work out the x,y 2d coodinates of those points (from the perspective of the viewer).

Anyone got any suggestions on how I should do this?

I have this method that I call from the vtkChartXYZ::Paint method. I want to get the missing bit that I’ve written a comment for:

```
void vtkChartXYZ::DrawAxesMidPoints(vtkContext2D* painter) {
    vtkContext3D* context = painter->GetContext3D();

    context->ApplyPen(this->Pen);

    for (int axis = 0; axis < 3; axis++) {

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                float midpoint[3];
                midpoint[0] = (axis == 0) ? 0.5 : i;
                midpoint[1] = (axis == 1) ? 0.5 : (axis == 0) ? i : j;
                midpoint[2] = (axis == 2) ? 0.5 : j;

                this->Box->TransformPoint(midpoint, midpoint);

                context->DrawPoint(vtkVector3f(midpoint));

                // x, y, z coordinates (3d)
                cout << "midpoint " << midpoint[0] << " " << midpoint[1] << " " << midpoint[2] << endl;
                
                // I want to work out the 2d viewer's perspective x and y coordinates...
            }
        }
    }

    context->ApplyPen(this->AxisPen);
}
```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [June 1, 2020, 10:18pm UTC](https://discourse.vtk.org/t/getting-2d-coordinates-from-3d-point/3425/2 "2020-06-01T22:18:29Z")

</div>

Hi, Matthew,

I tried the opposite of you want before (picking). But for getting screen coordinates from world coordinates, likewise, I suggest taking a look at the documentation of `vtkViewport` class: [https://vtk.org/doc/nightly/html/classvtkViewport.html](https://vtk.org/doc/nightly/html/classvtkViewport.html) . You can set the world coordinates with, say `SetWorldPoint()`, convert by calling `WorldToDisplay()` and collect the result with `GetDisplayPoint()`. You can access those methods via an instance of `vtkRenderer` (it’s a subclass of `vtkViewport`) that certainly exists somewhere in your program.

There are API for other types of coordinates there. See wich pair of them suits you.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![mdaley](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mdaley/32/1872_2.png) [@mdaley](https://discourse.vtk.org/u/mdaley)
#### Post date: [June 6, 2020, 11:01am UTC](https://discourse.vtk.org/t/getting-2d-coordinates-from-3d-point/3425/3 "2020-06-06T11:01:36Z")

</div>

Hi Paulo,

Thanks very much for the suggestion. I shall give that a try.

Kind regards,

Matt

---

<div class="post-metadata">

### Author: ![mdaley](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mdaley/32/1872_2.png) [@mdaley](https://discourse.vtk.org/u/mdaley)
#### Post date: [June 6, 2020, 2:22pm UTC](https://discourse.vtk.org/t/getting-2d-coordinates-from-3d-point/3425/4 "2020-06-06T14:22:15Z")

</div>

Hi again, your suggestion certainly helped; and I also remembered the important point: ‘read the f$%@ing manual’. What did was transform my float xyz world coordinate to a 2d view by using vtkCoordinate like this:

> vtkNew coord;  
> coord-\>SetCoordinateSystemToWorld();  
> coord-\>SetViewport(this-\>GetScene()-\>GetRenderer());  
> coord-\>SetValue(world\_xyz[0], world\_xyz[1], world\_xyz[2]);  
> coord-\>SetCoordinateSystemToDisplay();
> 
> double\* xy = coord-\>GetValue();

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [June 6, 2020, 8:08pm UTC](https://discourse.vtk.org/t/getting-2d-coordinates-from-3d-point/3425/5 "2020-06-06T20:08:09Z")

</div>

Hello,

I don’t think RTFM applies here 🙂. I had to scratch my head over the `vtkViewport`'s docs for a while to figure that out. It’s likely an older design not-so-obvious to use. Picking (the opposite opperation), for instance, is more straightforward.

cheers,

Paulo
