Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7727023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:21:21+00:00 2026-06-01T05:21:21+00:00

I want to do a simulation on Fraunhofer diffraction and for that purpose I

  • 0

I want to do a simulation on Fraunhofer diffraction and for that purpose I chose to use numpy and matplotlib. What I need to do, is to specify a 2D aperture function and for that I can create a meshgrid of x and y values and assign a function z(x,y), which in this case should be complex. All this does not sound too complicated, but here’s where I bump into a problem.

How do you define a rectangular, or a triangular piece inside a meshgrid, such that inside the geometric figure z=1 and outside z=0?

Minimal working example, where to begin:

#! /bin/usr/env python

# Import environment
import numpy as np

x_ = np.linspace(0,1,255)
y_ = np.linspace(0,1,255)
x,y = np.meshgrid(x_,y_)

what to do next?

I tried to solve the problem differently:

  • draw a figure using matplotlib
  • encode the values of z by using different colours
  • save the figure as a png
  • import the png as a numpy array and decode the colours.

However, this puts severe restrictions on the values taken by the function z, which is the main reason I am searching for a different approach.

Thanks very much for anybody who can help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:21:22+00:00Added an answer on June 1, 2026 at 5:21 am

    The two easiest options are to either use matplotlib.nxutils.points_inside_poly or to use mahotas.polygon.fill_polygon. The latter is a bit faster, but requires installing mahotas.

    As an example of the first option:

    import numpy as np
    from matplotlib.nxutils import points_inside_poly
    
    nx, ny = 10, 10
    poly_verts = [(1,1), (5,1), (5,9),(3,2),(1,1)]
    
    # Create vertex coordinates for each grid cell...
    # (<0,0> is at the top left of the grid in this system)
    x, y = np.meshgrid(np.arange(nx), np.arange(ny))
    x, y = x.flatten(), y.flatten()
    
    points = np.vstack((x,y)).T
    
    grid = points_inside_poly(points, poly_verts)
    grid = grid.reshape((ny,nx))
    
    print grid
    

    Which yields (a boolean numpy array):

    [[False False False False False False False False False False]
     [False  True  True  True  True False False False False False]
     [False False False  True  True False False False False False]
     [False False False False  True False False False False False]
     [False False False False  True False False False False False]
     [False False False False  True False False False False False]
     [False False False False False False False False False False]
     [False False False False False False False False False False]
     [False False False False False False False False False False]
     [False False False False False False False False False False]]
    

    On a side note, nxutils is going to be depreciated at some point in favor of some of the path methods. In the future, you’ll probably want to do something along the lines of:

    from matplotlib import path
    
    ...
    p = path.Path(poly_verts)
    grid = p.contains_points(points)
    ...
    

    However, that’s only in the github head at the moment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For use in a rigid body simulation, I want to compute the mass and
Suppose I want perform a simulation using the following function : fn1 <- function(N)
I'm writing a simulation of Bananagrams for fun. I want to use concurrency but
I want to run a simulation that uses as parameter a value generated from
I have a simple link click simulation that I want to do using jQuery.
I'm building a program that uses weather simulation using OpenGL. I want to include
I wrote several simulation programs in C++ and want to connect their outputs/inputs with
At the end of a simulation, I want to write some results as an
Situation: I want to provide a website service where users can enter some data
I want to build a one-dimensional model simulation of water temperature in a lake.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.