You need a combination of scale and translation matrices. You could first go to a "normalized" screen space (origin at 0,0 and scaling to 1,1) using, in pseudo-code:
MatToNormalized = Translation(-1, -1) x Scale(1/2, 1/4)
Then you can easily map to any kind of screen space. E.g. for question 1.:
MatToFullscreen = MatToNormalized x Scale(600, 500)
And for question 2.:
MatToViewport = MatToNormalized x Scale(300, 300) x Translation(100, 100)
EDIT
A Scilab dump with the actual matrices:
MatToNormalized = [1 0 0; 0 1 0; -1 -1 1] * [1/2 0 0; 0 1/4 0; 0 0 1]
0.5 0. 0.
0. 0.25 0.
- 0.5 - 0.25 1.
MatToFullscreen = MatToNormalized * [600 0 0; 0 500 0; 0 0 1]
300. 0. 0.
0. 125. 0.
- 300. - 125. 1.
TestMinFullScreen = [1 1 1] * MatToFullscreen
0. 0. 1.
TestMaxFullScreen = [3 5 1] * MatToFullscreen
600. 500. 1.
MatToViewport = MatToNormalized * [300 0 0; 0 300 0; 0 0 1] * [1 0 0; 0 1 0; 100 100 1]
150. 0. 0.
0. 75. 0.
- 50. 25. 1.
TestMinViewport = [1 1 1] * MatToViewport
100. 100. 1.
TestMaxViewport = [3 5 1] * MatToViewport
400. 400. 1.
Please note that Scilab uses a column-major notation, so if you want the row-major notation, just transpose the result ('
is Scilab's transpose operator):
MatToFullscreen'
300. 0. - 300.
0. 125. - 125.
0. 0. 1.
MatToNormalized'
0.5 0. - 0.5
0. 0.25 - 0.25
0. 0. 1.
If you don't get why the translation and scale matrices are formatted this way, try Wikipedia.