Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new primitive: linear-transform-graphics #241

Merged
merged 6 commits into from
Dec 13, 2020

Conversation

monaqa
Copy link
Contributor

@monaqa monaqa commented Oct 3, 2020

(graphics version of #218.)

This PR adds a new primitive
linear-transform-graphics: float -> float -> float -> float -> graphics -> graphics.
gr-origin |> linear-transform-graphics a b c d is evaluated to a graphics value into which the gr-origin is transformed with the following formula:

Combining linear-transform-graphics with shift-graphics, you can implement any graphics-to-graphics affine transform (rotation, scaling, reflection, etc.).

This PR also provides two functions, rotate-graphics and scale-graphics, in gr package.

There are two differences between linear-transform-path and linear-transform-graphics:

  • linear-transform-graphics can transform any inline-boxes using draw-text primitive.
  • When you transform a stroke with linear-transform-graphics command, the stroke thickness is changed (unless a congruence transformation).

@monaqa
Copy link
Contributor Author

monaqa commented Oct 3, 2020

Code example:

@require: stdjareport
@require: gr

let-inline ctx \test deg =
  let width = 1cm in
  let height = 3cm in
  inline-graphics width height 0pt (fun ((x, y) as pt) -> (
    let path = Gr.polygon pt [(x +' width, y); (x +' width, y +' height)] in
    let center = (x +' width *' 0.5, y +' height *' 0.5) in
    [
      stroke 0.5pt Color.black path;
      stroke 1pt Color.red path |> Gr.rotate-graphics center deg;
    ]
  ))
in

document (|
  title = {Linear Transformation of graphics};
  author = {\@monaqa};
  show-title = true;
  show-toc = false;
|) '<
  +p{
    ${30^\circ}: \test(30.0);
  }
  +p{
    ${60^\circ}: \test(60.0);
  }
  +p{
    ${120^\circ}: \test(120.0);
  }
  +p{
    ${300^\circ}: \test(300.0);
  }
>

Result:
linear-transform-graphics-demo

(Since rotation is part of the congruent transformation, the result appears to be the same as when using linear-transform-path.)

@monaqa
Copy link
Contributor Author

monaqa commented Oct 3, 2020

I forgot to mention this change: https://github.com/gfngfn/SATySFi/pull/241/files#diff-ff6ba46343bcea35973ad4d439f43e94L217-R261

In the previous code, the coordinates specified by the cm command are applied to all subsequent text (because the cm command precedes the q command). So I switched the order of the two commands.
Since the cm command currently performs only identity transformation, discarding this changes will not cause any problems.

@gfngfn
Copy link
Owner

gfngfn commented Dec 13, 2020

Looks very well-written, and I confirmed that the modified implementation indeed works as expected! (especially the composition of transformations and the rotation of texts)

@require: stdjareport
@require: gr

let-inline ctx \test deg =
  let width = 1cm in
  let height = 3cm in
  inline-graphics width height 0pt (fun ((x, y) as pt) -> (
    let path = Gr.polygon pt [(x +' width, y); (x +' width, y +' height)] in
    let center = (x +' width *' 0.5, y +' height *' 0.5) in
    let gr-text = draw-text (x, y) (read-inline ctx {あいう}) in
    [
      stroke 0.5pt Color.black path;
      stroke 1pt Color.red path |> Gr.rotate-graphics center deg;
      stroke 1pt Color.blue path |> Gr.rotate-graphics center deg |> Gr.rotate-graphics center deg;
      gr-text;
      gr-text |> Gr.rotate-graphics center deg;
      gr-text |> Gr.rotate-graphics center deg |> Gr.rotate-graphics center deg;
    ]
  ))
in

document (|
  title = {Linear Transformation of graphics};
  author = {\@monaqa};
  show-title = true;
  show-toc = false;
|) '<
  +p{${30^\circ}: \test(30.0);}
  +p{${60^\circ}: \test(60.0);}
  +p{${120^\circ}: \test(120.0);}
  +p{${300^\circ}: \test(300.0);}
>

スクリーンショット 2020-12-14 1 18 25

Thank you so much for enhancement!

@gfngfn gfngfn merged commit bf75fd9 into gfngfn:master Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants