kill(all)$

maxima : intersection 2d grid & circle ,version 1.0
peter.vlasschaert@gmail.com,23/01/2021

use : finite difference method

load packages & option
===================

load(draw)$

ratprint:false$

given : circle & 2d grid
==================

R:2;     /* radius of cicle */
hx:0.5;  /* grid value : x-direction */
hy:0.5;  /* grid value : y-direction */

x-direction : => y-segment, distance from the circle .
=========

nuptx(ii):=sqrt((R^2-(ii*hx)^2)/hy^2);

if  R ≥ x^2+ y^2

p1:makelist(nuptx(ii),ii,0,R/hx);

number of points : y - direction,  for : x>0,y >0

p2:floor(%-mod(%,hy));

p2a:makelist(floor(p1[ii]-mod(p1[ii],hy)),ii,1,length(p1));

max y values ( with respect : x - direction)

p3:hy*p2a;

p3[1];

intersection : circle -> x-direction , y values 

p4:makelist(sqrt(R^2-(ii*hx)^2),ii,0,length(p1)-1);

→ segment : max y values

p4a:p4-p3;

y-direction : => x-segment , distance from the circle .
=========
use : p3

first point : highest values :        (1*hx)
--------------------------------------------------

p1b:sqrt(R^2-(p3[2])^2);

p1c:p1b-1*hx;

if p1c > hx  then 1 else p1c 

→ length: x -segment

p1d:1;

second point : highest values : (2*hx)
-------------------------------------------------

p1b1:sqrt(R^2-(p3[3])^2);

p1c1:p1b1-2*hx;

if p1c1 > hx  then 1 else p1c1 

→ length: x -segment

p1d1:p1c1;

third point : highest values :     (3*hx)
-------------------------------------------------

p1b2:sqrt(R^2-(p3[4])^2);

p1c2:p1b2-3*hx;

if p1c2 > hx  then 1 else p1c2 

→ length: x -segment

p1d2:p1c2;

p1dd:[p1d,p1d1,p1d2];

same hx = hy : grid division in both directions
------------------------------------------------------------
x = segment , xs :'array' 
use : p3 ,'array'

nupo: R/hx -1;

sx(k):=sqrt(R^2-(p3[k+1])^2);

xs:makelist(0,ii,1,nupo);

for i:1 thru nupo do
  (qs:sx(i)-i*hx,
    if qs > hx then xs[i]:1 else xs[i]:qs
    
   )$

→x-segment : i point , i=1..nupo

xs;

check : between p1dd & xs

is(p1dd=xs);

-------------------------------------------------------------------------------------------------------------

extreme (x,y),values
---------------------------

first quadrant (two fold symmetry : angle 45 degrees , axes :symmetry)
-----------------

→ 3 points

 =>  x: values

x1:makelist(ii*hx,ii,1,nupo);

=>  y: values

y1:makelist(p3[ii],ii,2,nupo+1);

xy1:makelist([x1[ii],y1[ii]],ii,1,nupo);

other quadrants (four fold symmetry)
---------------------
first       quadrant :(+,+) → xy1,list
second  quadrant :(-,+) → xy2,list
thrid       quadrant :(-,-)  → xy3,list
fourth     quadrant :(+,-) → xy4,list

xy2:makelist([-x1[ii],+y1[ii]],ii,1,nupo);
xy3:makelist([-x1[ii],-y1[ii]],ii,1,nupo);
xy4:makelist([+x1[ii],-y1[ii]],ii,1,nupo);

all max-min values:  ( x & y : inside the circle)
-------------------------------------------------------------

xyv:flatten([xy1,xy2,xy3,xy4]);

xy:makelist([xyv[2*(i-1)+1],xyv[2*i]],i,1,nupo*4);/* odd & even */

xyl:length(xy);

How to draw : circle & extreme points (xy)
================================

wxplot2d([[parametric, R*cos(t), R*sin(t), [t,0,2*%pi]],
     [discrete, xy]],[style,lines,points],grid2d,
     [x, -R, R],[y,-R,R],[color,red,blue],
     same_xy)$       

wxplot2d([[parametric, R*cos(t), R*sin(t), [t,0,2*%pi]],
[discrete, xy ]], [style, [lines,1,1],[points,5,2,6]],grid2d ,
[x, -R, R],[y,-R,R],[color,red,blue], [legend,"circle","xy points"],
[xlabel,"x-axis"], [ylabel,"y-axis"],[title, "extreme values,inside xy"],
 same_xy);     

 => use draw :same above.

draw2d(
    dimensions = [400,400],
    grid = true,
    color = blue,
    line_width = 2,
    implicit(x^2+y^2=R^2,x,-R,R,y,-R,R),
    color = forest_green,
    point_size = 3,
    point_type = filled_circle,
    points(xy)  
    )$

→ To get : Figure 1

1e) copy to clipboard
2e) Ctrl V

→ rem :wxdraw2d , we get not circle ( = ellipse)

set_draw_defaults( grid=true)$

wxdraw2d(   
    dimensions = [400,400],
    color = blue,
    line_width = 2,
    implicit(x^2+y^2=R^2,x,-R,R,y,-R,R),
    color = forest_green,
    point_size = 1,
    point_type = filled_circle,
    points(xy),
    proportional_axes = 'xy
        )$

How to draw a line (draw2d)
======================

wxdraw2d(
    points_joined = true,
    line_width    = 2,
    color         = red,
    points([xy[3],xy[9]]),
    proportional_axes = 'xy 
        )$

find grid points inside circle with radius R
================================

v:[]$

for xx:-2 thru 2 step hx do(
  for yy:-2 thru 2 step hy do(
    if xx^2+yy^2 < R^2 then
     v:endcons([xx,yy],v)    ) 
                            )$

v;

wxdraw2d(   
    dimensions = [400,400],
    color = blue,
    line_width = 2,
    implicit(x^2+y^2=R^2,x,-R,R,y,-R,R),
    color = forest_green,
    point_size = 1,
    point_type = filled_circle,
    points(v),
    proportional_axes = 'xy
        )$

wxdraw2d(   
    dimensions = [400,400],
    color = blue,
    line_width = 2,
    implicit(x^2+y^2=R^2,x,-R,R,y,-R,R),
    color = forest_green,
    point_size = 1,
    point_type = filled_circle,
    points(v),
    color = red,
    point_size = 2,
    point_type = filled_circle,
    points(xy),
    proportional_axes = 'xy
        )$

find grid points on the circle and outside the circle
=======================================

w:[]$

for xx:-2 thru 2 step hx do(
  for yy:-2 thru 2 step hy do(
    if xx^2+yy^2 ≥ R^2 then
     w:endcons([xx,yy],w)    ) 
                            )$

w;

wxdraw2d(   
    dimensions = [400,400],
    color = blue,
    line_width = 2,
    implicit(x^2+y^2=R^2,x,-R,R,y,-R,R),
    color = forest_green,
    point_size = 1,
    point_type = filled_circle,
    points(v),
    color = red,
    point_size = 2,
    point_type = filled_circle,
    points(xy),
    color = orange,
    point_size = 2,
    point_type = filled_circle,
    points(w),
    proportional_axes = 'xy
        )$