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

Switching API to ints #83

Merged
merged 6 commits into from
Jan 26, 2024
Merged

Switching API to ints #83

merged 6 commits into from
Jan 26, 2024

Conversation

FayCarsons
Copy link
Collaborator

Switched API to ints, including colors and context initialization.

Copy link
Collaborator

@nikochiko nikochiko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I've left a bunch of questions and changes.
Please take a loook.

examples/axes.ml Outdated Show resolved Hide resolved
Comment on lines 4 to 5
let resolution = (1200., 900.)
let tmap f (x, y) = (f x, f y)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we define resolution as a tuple of ints instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it were ints I would need to add 5-6 more calls to float_of_int, as opposed to the 1 call to tmap int_of_float resolution that's present.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where would that be required?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 40 in rand_circle. I think this example needs to be rewritten from scratch though, it doesn't properly pack the circles since migrating to the Cairo backend and I think it could be made a bit more concise.

Comment on lines +107 to +108
~c:(point (int_of_float x) (int_of_float y))
(int_of_float radius)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead change pack_circles and make_concentric to give us ints?

Also, instead of shrink, can you try using transform.scale to have the same effect?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't use Transform.scale because I can't test the radii of the circles as they aren't exposed in the API. Also, that would require switching from the current representation of circles as (float * float) * float tuples.

I can make make_concentric and pack_circles take and return ints but that would require a lot of conversion back into floats because the math required to check if circles overlap is done with floats.

I think that, in general, this sketch is illustrative of what an end-user writing a more complex sketch will look like.

examples/complex.ml Show resolved Hide resolved
examples/circle_row_joy.ml Show resolved Hide resolved
let ctx = Cairo.create surface in
Cairo.scale ctx x y;
Cairo.scale ctx (float_of_int w) (float_of_int h);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

If it scales un-evenly (w != h), would it convert our circles to ellipses?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just sets the initial scale to 1/1.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is buggy: #86

lib/context.mli Show resolved Hide resolved
@@ -1,6 +1,6 @@
let context = Context.context

type point = Shape.point
type 'a point = 'a Shape.point
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this type change aim to do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it easier to convert between/handle the int points received from the end user

lib/render.ml Outdated Show resolved Hide resolved
lib/render.ml Outdated
let radius = radius /. min (fst ctx.size) (snd ctx.size) in
let size = tmap float_of_int ctx.size in
let x, y = scale_point size c in
let radius = radius /. min (fst size) (snd size) in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we change the radius like this?

I don't think this would give us what we want on a rectangular canvas.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be scaled down to a 0-1 range some way, I'm not sure what a better alternative is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm currently co-working with a friend who is a professional graphics programmer, she thinks it should be this radius /. (sqrt (Float.pow width 2. +. Float.pow height 2.) /. 2.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on how we convert one unit of joy-land into one unit of cairo.
If you look at the new ticket I created - #86 - this is caused because we are converting a (float_in_range[-150..150], float_in_range[-250..250]) to (float_in_range[0..1], float_in_range[0..1]). It wouldn't be right to map a rectangle canvas to a square canvas. We should map to a rectangular canvas.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I learnt that we don't need to map it to [0..1] either.
https://github.com/Chris00/ocaml-cairo/blob/1a347fb5d7cc887121bc335ecd3dfc68a79f0318/examples/stroke.ml#L6-L18

We can denormalize points to ([0..w], [0..h]) and that would be okay if we omit Cairo.scale w h later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand how that example shows that, the coordinates are all in [0, 1]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coordinates are scaled in [0..1] with respect to that axis. So scaling a Joy coordinate space of size [200, 300] to [0..1] on both axes does not scale it into a square aspect ratio. No resolution is 'lost', so to speak, as those [0..1] ranges mean different things for each axis.

This code also just gives us a static radius for any given circle, I feel confident that it isn't what's causing the distortions we're seeing in non-square aspect ratios.

@nikochiko
Copy link
Collaborator

Thanks! @FayCarsons

This looks good to me.

@nikochiko nikochiko merged commit 8fffcb5 into Sudha247:main Jan 26, 2024
0 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants