in reply to Fitting N Xterms On An X by Y screen

Given there's no constraint about overlap, the simple solution is to have all xterms the same size, and the size of the screen. And if you add a constraint about non-overlapping, there still isn't a constraint all xterms need to be visible: I'll take one xterm the size of the screen, place it on the screen, and put the rest of the xterms out of the viewpoint.

It's a harder question if you add the constraints:

The latter basically means the exercise is more than just tiling a simple rectangle. But it may mean the problem becomes NP-complete.
  • Comment on Re: Fitting N Xterms On An X by Y screen

Replies are listed 'Best First'.
Re^2: Fitting N Xterms On An X by Y screen
by Anonymous Monk on Jun 05, 2009 at 10:01 UTC
      This is actually for a xchat script to deal with channel windows so that won't work :)

        This seems to a fairly reasonable job for upto 26 windows(1) and most reasonable combinations of screen width and height (supplied on the command line). The code could definitely do with refactoring, but it is always going to be a bit messy as there are so many corner and edge cases. Literally.

        Don't specifiy a width greater than you own terminal-1 otherwise it just looks a mess.

        #! perl -sw use 5.010; use strict; use Data::Dump qw[ pp ]; my( $w, $h ) = @ARGV; sub display { my @screen = map ' ' x $w . '|', 0 .. $h; my $char = 'A'; for my $term ( @_ ) { substr( $screen[ $_ ], $term->[ 0 ], $term->[ 1 ] ) = $char x $term->[ 1 ] for $term->[ 2 ] .. $term->[ 2 ] + $term->[ 3 ]; ++$char; } # system 'cls'; say for @screen; } for my $n ( 1 .. 26 ) { my @xterms; my $xn = int( $n ** 0.5 ); my $yn = int( $n / $xn ); my $rem = $n - ($xn * $yn); my $deltaX= int( $w / $xn ); my $deltaY = int( $h / ( $yn + ( $rem ? 1 : 0 ) ) ); my( $tlx, $tly ) = ( 0, 0 ); for my $t ( 1 .. $n ) { push @xterms, [ $tlx, $deltaX, $tly, $deltaY ]; $tlx += $deltaX; if( ( $tlx + $deltaX ) > $w ) { $xterms[ -1 ][ 1 ] = $w - $xterms[ -1 ][ 0 ]; $tlx = 0; $tly += $deltaY; my $xLeft = $n - $t; if( $xLeft and $xLeft <= $xn ) { $deltaX = int( $w / $xLeft ); $deltaY = $h - $tly; } } } display( @xterms ); <STDIN>; } __END__ C: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EEEEEEEE| EEEEEEEEEEEEEEEEEEEE|

        (1) The demo gets way more complex once you move to more that a single character for each screen.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.