1. unit axis intercepts [solved]

I've written a natty little routine which bisects a bezier curve into sub-pixel fragments and that
part is working fine, and once I've got {x1,y1} and {x2,y2} within a 1x1 pixel square, I want to
know whether they intersect the unit axis, for which "ceil(min(x1,x2))!=ceil(max(x1,x2))" and the
same for the unit y axis is fine. However, I also want to know where it intercepts the unit axis,
which btw is floor(max(x1,x2)), and also incidentally I'm happy with the edge cases of 0.1..1 not
being an intercept but its partner 1..1.9 being one, and wouldn't want both deemed one anyway.
Once it is that small, I think it is safe to treat the curve fragment as a straight line.

I'm sure that's all clear as mud.

So I've got minx, maxx, unitx, ditto y, but in want of a hitux and hituy. I think the problem is
(flat|steep) * (\|/) * (up|down or left|right) and I'm currently staring at (a roughly estimated)
40 or so glitches in a test set of 625 cases and a brain clogged with min/max/ceil/floor/abs when
maybe I don't really need or want any of them?? I somehow believe there is a dirt-simple solution
itching to get out, but I just can't see it.

To recap/summarise, given some {x1,y1}, {x2,y2}: where does it hit a straddled whole x, ditto y?
You cannot make any assumptions about the first point being left or right or above or below the
other one, but you can assume they are strictly less than 1 apart, in both directions.

new topic     » topic index » view message » categorize

2. Re: unit axis intercepts

I asked ChatGPT, because my brain exploded.

It sounds like you want to determine whether a line segment defined by two points {x1, y1} and {x2, y2} intersects the unit x and y axes, and if it does, where it intersects them. Additionally, you want to handle the special case where a segment from 0.1 to 1 on the axis is not considered an intercept, but 1 to 1.9 is. Here's a possible approach to solve this problem:

1. Calculate the intersection points of the line segment with the unit x and y axes.

To find the intersection points with the unit x-axis, you can use the equation of the line:

y = m * x + b

Where m is the slope of the line and b is the y-intercept. In your case, you can calculate the slope as:

m = (y2 - y1) / (x2 - x1)

Then, calculate the y-coordinate of the intersection point with the unit x-axis (where x = 0):

y_intersection_x = m * 0 + b = b

So, you have found the intersection point with the unit x-axis, which is {0, y_intersection_x}.

To find the intersection point with the unit y-axis, you can use the same equation with x = 1:

y_intersection_y = m * 1 + b

2. Check if the intersection points are within the desired range.

You mentioned that you want to consider an intercept only if it's within certain ranges. For the unit x-axis, you can check if the y-coordinate of the intersection point is within the range [0.1, 1.9]. For the unit y-axis, you can check if the y-coordinate of the intersection point is within the range [0.1, 1.9].

Here's some Python-like pseudocode to implement this:

```python 
# Calculate the slope of the line 
m = (y2 - y1) / (x2 - x1) 
 
# Calculate the y-intercept 
b = y1 - m * x1 
 
# Calculate the intersection point with the unit x-axis 
y_intersection_x = b 
 
# Calculate the intersection point with the unit y-axis 
y_intersection_y = m * 1 + b 
 
# Check if the intersections are within the desired range 
if 0.1 <= y_intersection_x <= 1.9: 
    # Intersection with unit x-axis is within the desired range 
    hitux = True 
else: 
    hitux = False 
 
if 0.1 <= y_intersection_y <= 1.9: 
    # Intersection with unit y-axis is within the desired range 
    hituy = True 
else: 
    hituy = False 
 
``` 

This code should help you determine whether the line segment intersects the unit x and y axes within the specified ranges. If it does, the `hitux` and `hituy` variables will be set to True, indicating an intersection, and you can also retrieve the intersection points if needed.

new topic     » goto parent     » topic index » view message » categorize

3. Re: unit axis intercepts

I also asked it towrite the code in Euphoria

To determine where a Bezier curve intersects the unit axis and whether it straddles a whole pixel, you can follow these steps in Euphoria:

-- Function to find the intersection points with unit axis 
function findIntersectionPoints(x1, y1, x2, y2) 
    -- Determine whether the curve is flat or steep 
    flat = abs(y2 - y1) < abs(x2 - x1) 
     
    if flat then 
        -- Swap x and y coordinates for a flat curve 
        x1, y1 = y1, x1 
        x2, y2 = y2, x2 
    end 
     
    -- Determine whether the curve is going up or down 
    up = y1 > y2 
     
    -- Calculate the intersection point on the unit y axis 
    if up then 
        hituy = floor(y2) 
    else 
        hituy = ceil(y2) 
    end 
     
    -- Calculate the intersection point on the unit x axis 
    hitux = ((hituy - y1) / (y2 - y1)) * (x2 - x1) + x1 
     
    if flat then 
        -- Swap x and y coordinates back if it's a flat curve 
        hitux, hituy = hituy, hitux 
    end 
     
    -- Check if the intersection point straddles a whole pixel 
    straddles_x = ceil(x1) != ceil(x2) 
    straddles_y = ceil(y1) != ceil(y2) 
     
    return hitux, hituy, straddles_x, straddles_y 
end 
 
-- Example usage 
x1 = 0.5 
y1 = 0.5 
x2 = 1.5 
y2 = 1.9 
 
hitux, hituy, straddles_x, straddles_y = findIntersectionPoints(x1, y1, x2, y2) 
 
if straddles_x then 
    ? "X-axis intersection at ", hitux 
else 
    ? "No X-axis intersection" 
end 
 
if straddles_y then 
    ? "Y-axis intersection at ", hituy 
else 
    ? "No Y-axis intersection" 
end 

This code defines a `findIntersectionPoints` function that calculates where the Bezier curve intersects the unit axis (both X and Y) and whether it straddles a whole pixel. The function accounts for whether the curve is flat or steep and whether it's going up or down. It also provides example usage and prints the results.

new topic     » goto parent     » topic index » view message » categorize

4. Re: unit axis intercepts

Oh, i have no idea whether any of this makes sense, I just wanted to copy and paste your question into chatgpt and see what happened.

It also didn't want to play global thermonuclear with me, which was a bit disappointing.

Cheers

Chris

new topic     » goto parent     » topic index » view message » categorize

5. Re: unit axis intercepts

I give it 11/10 for "brazenly pretending to be useable"... It actually hit the nail on the head,
in that I was (stupidly) trying to use minx + something, when I should have been using
x1 +[/-] let the sign of the slope dictate which way. Anyway, so far, it's certainly progress.

When I tried, didn't get anything near as good, no idea why not (and boy was it slow).
Next time you use ChatGPT, you should try pasting errors from trying to run the code,
since that should (hopefully quite quickly) teach ChatGPT to emit better Phix code.

Thanks, Pete

new topic     » goto parent     » topic index » view message » categorize

6. Re: unit axis intercepts

ChrisB said...

It also didn't want to play global thermonuclear with me, which was a bit disappointing.

Wrong hardware. GTW was played on a massively configurable parallel Thinking Machine CM-5 (named WOPR in the movie, and was a prop, not an actual computer), of the sort NASA once used to descramble satellite images. The confusion may be because the company that made the Connection Machine was Thinking Machine Corporation. I don't know what they were thinking. The movie's game was ported to 8-bit computers of the early 1980's, and just this year Russia threatened to use nukes again so , still playable, i guess.

Kat

new topic     » goto parent     » topic index » view message » categorize

7. Re: unit axis intercepts

Heh heh.

Well spotted! (Miss Falcon?)

Cheers

Chris

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu