hageldave / JPlotter

OpenGL based 2D Plotting Library for Java using AWT and LWJGL
https://github.com/hageldave/JPlotter/wiki
MIT License
45 stars 6 forks source link

possibility to adapt view to aspect ratio of coordinate system #40

Open hageldave opened 2 years ago

hageldave commented 2 years ago

When resizing the canvas holding a CoordSysRenderer its coordinate view stays the same but is stretched visually to span over the available area. This distorts the contents of the coordinate system and can be undesired. See the example below where the coordinate view stays the same but the coordinate area changed and squishes the lines and distorts the shape.

matching view and viewport different aspect ratios desired behaviour
Screenshot from 2022-06-07 12-47-59 Screenshot from 2022-06-07 13-05-15 Screenshot from 2022-06-07 13-08-21

It is not always problematic when aspect ratios of view and viewport (coordsys area) do not match, but there should be a mechanism that automatically adapts the view to keep up with the areas aspect ratio.

Proposed mechanism: There will be a new field Rectangle2D desiredCoordinateView which holds the view that should be shown leastwise. The existing field Rectangle2D coordinateView is then sized accordingly so that desiredCoordinateView fits inside. This means that coordinateView is allowed to be either taller or wider than desiredCoordinateView and can be sized so it matches the aspect ratio of the coordinate system area (getCoordSysArea()).

For the automatic coordinate view adaption the CoordSysRenderer has to be made aware of 'componentResized(..)' events of its canvas. When the canvas is resized, the view is resized so that it has the same aspect ratio as the view area and contains the desired view.

Refined Mechanism Proposal: With the introduction of a second view field in CoordSysRenderer and the need for componentResized event awareness we complicate the API of the CoordSysRenderer class (which is already quite large). Instead we could introduce a new class for controlling CoordSysRenderer.coordinateView. We already have the interaction classes CoordSysPanning, CoordSysViewSelector and CoordSysScrollZoom that change the coordinate view and listen to events of the canvas. Similarly we could introduce a class CoordSysViewController that holds the field Rectangle2D desiredCoordinateView and adapts CoordSysRenderer.coordinateView when canvas is resized or desiredCoordinateView changes.

Implication: The already mentioned interaction classes for changing the view will have to use the new CoordSysViewController instead of directly manipulating the view. Otherwise changes to the view will be overwritten when component is resized.

hageldave commented 1 year ago

I implemented a very simple aspect ratio preserving mechanism in here: https://github.com/hageldave/uamds/blob/c6a07d9f5ed4187b9a258121d08dbf8b3e9cd65d/java/core/src/main/java/uamds/demo/LoFiScatter.java#L95