我试图比较两组坐标和绘图匹配项如果它们足够接近,但是代码无法编译?

如何解决我试图比较两组坐标和绘图匹配项如果它们足够接近,但是代码无法编译?

我正在尝试根据代码中的说明绘制可能的匹配项,但是代码未编译,因此我无法弄清楚错误是什么。我将不胜感激。

我的代码编辑器在代码的这一行中给我错误'解析错误:意外的令牌i'-> for(let i = 0 ; i < possibleMatches.length ; i++)

到目前为止,这是我在其中绘制了CrimeSightings和AttackRecorded的地图:

enter image description here

代码如下:

/*
Officer: 3357927
CaseNum: 601-2-69352192-3357927

Case 601 - Murdering Again - stage 3

Now murders are beginning to occur - we're pretty sure that this is the work of Fry.
If we can place her near any of the recent crime scenes in the area we should be able narrow down her location.

In the setup function,use a for loop to traverse the sightings,marking all of the locations on the map
where she was last seen. Do this by drawing small,Goldenrod fill rectangles centered over each location.

In addition,we've assembled a list of recent thefts in the area. Using another for loop to traverse the
recent crime records,you should mark those locations on the map. Do this by drawing small,DarkGoldenrod fill triangles centered over each location.

Use X11 colours. You can find a reference table at https://en.wikipedia.org/wiki/Web_colors.

Let's try to catch Fry by looking patterns between sightings and crimes. If she was within less than 97 pixels of any of the crimes then the details
should be pushed to possible matches with the following format.

{ crime:{x: 0,y:0,victimName: "John Doe"},suspect:{x: 0,y:0} }

Note that the possible matches are already being drawn.
Your job is simply to fill the array with the correct data.

For this mission you will need ONLY the following:

- for loop
- dist()
- if()
- fill
- rect() NB. Draw each rectangle with the point at its center.

- fill
- triangle() NB. Draw each triangle with the point roughly at its center.


*/

var countyMap;

var possibleMatches = [];

//Sightings of Casey Fry.

var CriminalSightings = [ 
  { Loc_X : 639,Loc_Y : 288},{ Loc_X : 681,Loc_Y : 286},{ Loc_X : 712,Loc_Y : 293},{ Loc_X : 756,Loc_Y : 310},{ Loc_X : 715,Loc_Y : 368},{ Loc_X : 701,Loc_Y : 425},{ Loc_X : 753,Loc_Y : 436},{ Loc_X : 815,Loc_Y : 468},{ Loc_X : 795,Loc_Y : 506},{ Loc_X : 788,Loc_Y : 497},{ Loc_X : 781,Loc_Y : 486},{ Loc_X : 768,Loc_Y : 489},{ Loc_X : 750,Loc_Y : 500},{ Loc_X : 732,{ Loc_X : 714,Loc_Y : 514},{ Loc_X : 695,Loc_Y : 531},{ Loc_X : 693,Loc_Y : 552},{ Loc_X : 654,Loc_Y : 523},{ Loc_X : 624,{ Loc_X : 594,Loc_Y : 484},{ Loc_X : 555,Loc_Y : 474} 
];


//Recent crime records.

var AttackRecorded = [ 
  { coordinate_x : 409,coordinate_y : 446,victim_name : 'JENIFFER DEAUVILLE'},{ coordinate_x : 443,coordinate_y : 419,victim_name : 'JACQUELINE DURANTS'},{ coordinate_x : 465,coordinate_y : 548,victim_name : 'BRAD SILVEIRA'},{ coordinate_x : 709,coordinate_y : 552,victim_name : 'DRUSILLA WARMAN'},{ coordinate_x : 695,coordinate_y : 421,victim_name : 'LINETTE MOHWAWK'},{ coordinate_x : 652,coordinate_y : 268,victim_name : 'SUMMER CASIMERE'},{ coordinate_x : 641,coordinate_y : 306,victim_name : 'NELSON TINTLE'},{ coordinate_x : 119,coordinate_y : 344,victim_name : 'HANG NIEMELA'},{ coordinate_x : 114,coordinate_y : 359,victim_name : 'LAVERNE JACQUELIN'},{ coordinate_x : 90,coordinate_y : 490,victim_name : 'JESSIA PORTOS'},{ coordinate_x : 76,coordinate_y : 516,victim_name : 'TU DAVISWOOD'},{ coordinate_x : 615,coordinate_y : 741,victim_name : 'LARRAINE PEGORD'},{ coordinate_x : 349,coordinate_y : 796,victim_name : 'LIANNE COURTWOOD'},{ coordinate_x : 456,coordinate_y : 770,victim_name : 'NICOLE ASHELY'} 
];


function preload()
{
    countyMap = loadImage("map.png")
}

function setup()
{
  createCanvas(countyMap.width,countyMap.height);

    image(countyMap,0);

    //add your code below here
    noFill();
    stroke(218,165,32);
    for (i = 0; i < CriminalSightings.length; i++){
        var sighting = CriminalSightings[i]
        
        rect(sighting.Loc_X - 5,sighting.Loc_Y - 5,5,5);

        }
    stroke(184,134,11);
    for (j = 0; j < AttackRecorded.length; j++){
        var record = AttackRecorded[j]
        
        triangle(record.coordinate_x,record.coordinate_y -5,record.coordinate_x - 5,record.coordinate_y + 5,record.coordinate_x + 5,record.coordinate_y + 5  );

        }
    
   for(var i =0; i<CriminalSightings.length; i++ ) 
   {
       for(var j = 0;AttackRecorded.length; j++ )
           {
       if(dist(CriminalSightings[i].Loc_X,CriminalSightings[i].Loc_Y,AttackRecorded[j].coordinate_x,AttackRecorded[j].coordinate_y) < 97)
        {
            possibleMatches.push
            ({ crime:{x: AttackRecorded[j].coordinate_x,y:AttackRecorded[j].coordinate_y,victimName: AttackRecorded[j].victim_name},suspect:{x: CriminalSightings[i].Loc_X,y:CriminalSightings[i].Loc_Y} })
        }
            }
   }
    // code to draw the matches ( if any)
    for(let i = 0 ; i < possibleMatches.length ; i++)
    {
        stroke(127);
        strokeWeight(3);
        line(possibleMatches[i].crime.x,possibleMatches[i].crime.y,possibleMatches[i].suspect.x,possibleMatches[i].suspect.y);

        noStroke();
        fill(127);
        text(possibleMatches[i].crime.victimName,possibleMatches[i].crime.x + 15,possibleMatches[i].crime.y + 15);
    }
}

//We are not using the draw function this time

解决方法

如果您查看浏览器的JavaScript控制台,则会看到错误。

在这种情况下,它是:YourSketchName.js:### Uncaught TypeError: Cannot read property 'coordinate_x' of undefined将是行号,您可以单击第一个错误位置,然后查看它在代码中的位置,例如:

if (dist(CriminalSightings[i].Loc_X,CriminalSightings[i].Loc_Y,AttackRecorded[j].coordinate_x,AttackRecorded[j].coordinate_y) < 97)

错误的意思是,它期望一个具有coordinate_x属性的对象,而它却拥有undefined。缺少什么,但是为什么呢?

通常,当发生这种情况时,有助于查看附近的线条以获取上下文:

for (var i =0; i < CriminalSightings.length; i++ ) 
  {
    for (var j = 0; AttackRecorded.length; j++ )
    {
      if (dist(CriminalSightings[i].Loc_X,AttackRecorded[j].coordinate_y) < 97)
      {
      }
    }
  }

您确实有两个计数器,您要遍历的每个数组都有一个计数器,但是错字了。

您可以发现以下两者之间的区别:

for (var i =0; i < CriminalSightings.length; i++ ) 

for (var j = 0; AttackRecorded.length; j++ )

您缺少正确的条件:

for (var j = 0; j < AttackRecorded.length; j++ )

隐藏的部分有一个问题,就是AttackRecorded.length是一个大于0的数字:在if条件下,这将被评估为true。这意味着j计数器将增加到13:AttackRecorded数组的最后一个有效索引,因此为undefined(从索引14开始没有任何内容)。

您可以运行测试波纹管。运行代码段后,请使用“整页”链接或滚动条在右下方查看距离图。

var countyMap;

var possibleMatches = [];

//Sightings of Casey Fry.

var CriminalSightings = [ 
  { Loc_X : 639,Loc_Y : 288},{ Loc_X : 681,Loc_Y : 286},{ Loc_X : 712,Loc_Y : 293},{ Loc_X : 756,Loc_Y : 310},{ Loc_X : 715,Loc_Y : 368},{ Loc_X : 701,Loc_Y : 425},{ Loc_X : 753,Loc_Y : 436},{ Loc_X : 815,Loc_Y : 468},{ Loc_X : 795,Loc_Y : 506},{ Loc_X : 788,Loc_Y : 497},{ Loc_X : 781,Loc_Y : 486},{ Loc_X : 768,Loc_Y : 489},{ Loc_X : 750,Loc_Y : 500},{ Loc_X : 732,{ Loc_X : 714,Loc_Y : 514},{ Loc_X : 695,Loc_Y : 531},{ Loc_X : 693,Loc_Y : 552},{ Loc_X : 654,Loc_Y : 523},{ Loc_X : 624,{ Loc_X : 594,Loc_Y : 484},{ Loc_X : 555,Loc_Y : 474} 
];


//Recent crime records.

var AttackRecorded = [ 
  { coordinate_x : 409,coordinate_y : 446,victim_name : 'JENIFFER DEAUVILLE'},{ coordinate_x : 443,coordinate_y : 419,victim_name : 'JACQUELINE DURANTS'},{ coordinate_x : 465,coordinate_y : 548,victim_name : 'BRAD SILVEIRA'},{ coordinate_x : 709,coordinate_y : 552,victim_name : 'DRUSILLA WARMAN'},{ coordinate_x : 695,coordinate_y : 421,victim_name : 'LINETTE MOHWAWK'},{ coordinate_x : 652,coordinate_y : 268,victim_name : 'SUMMER CASIMERE'},{ coordinate_x : 641,coordinate_y : 306,victim_name : 'NELSON TINTLE'},{ coordinate_x : 119,coordinate_y : 344,victim_name : 'HANG NIEMELA'},{ coordinate_x : 114,coordinate_y : 359,victim_name : 'LAVERNE JACQUELIN'},{ coordinate_x : 90,coordinate_y : 490,victim_name : 'JESSIA PORTOS'},{ coordinate_x : 76,coordinate_y : 516,victim_name : 'TU DAVISWOOD'},{ coordinate_x : 615,coordinate_y : 741,victim_name : 'LARRAINE PEGORD'},{ coordinate_x : 349,coordinate_y : 796,victim_name : 'LIANNE COURTWOOD'},{ coordinate_x : 456,coordinate_y : 770,victim_name : 'NICOLE ASHELY'} 
];


//function preload()
//{
//    countyMap = loadImage("map.png")
//}

function setup()
{
  // placeholder for map.png,for demo purposes only
  countyMap = createImage(900,900);
  createCanvas(countyMap.width,countyMap.height);

    image(countyMap,0);

    //add your code below here
    noFill();
    stroke(218,165,32);
    for (i = 0; i < CriminalSightings.length; i++){
        var sighting = CriminalSightings[i]
        
        rect(sighting.Loc_X - 5,sighting.Loc_Y - 5,5,5);

        }
    stroke(184,134,11);
    for (j = 0; j < AttackRecorded.length; j++){
        var record = AttackRecorded[j]
        
        triangle(record.coordinate_x,record.coordinate_y -5,record.coordinate_x - 5,record.coordinate_y + 5,record.coordinate_x + 5,record.coordinate_y + 5  );

        }
    
   for(var i =0; i<CriminalSightings.length; i++ ) 
   {
       for(var j = 0; j < AttackRecorded.length; j++ )
           {
       if(dist(CriminalSightings[i].Loc_X,AttackRecorded[j].coordinate_y) < 97)
        {
            possibleMatches.push
            ({ crime:{x: AttackRecorded[j].coordinate_x,y:AttackRecorded[j].coordinate_y,victimName: AttackRecorded[j].victim_name},suspect:{x: CriminalSightings[i].Loc_X,y:CriminalSightings[i].Loc_Y} })
        }
            }
   }
    // code to draw the matches ( if any)
    for(let i = 0 ; i < possibleMatches.length ; i++)
    {
        stroke(127);
        strokeWeight(3);
        line(possibleMatches[i].crime.x,possibleMatches[i].crime.y,possibleMatches[i].suspect.x,possibleMatches[i].suspect.y);

        noStroke();
        fill(127);
        text(possibleMatches[i].crime.victimName,possibleMatches[i].crime.x + 15,possibleMatches[i].crime.y + 15);
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.min.js"></script>

我建议您查看Kevin Workman's How to Debug Tutorial:它非常擅长于说明如何将较大的问题分解为较小的块,以及一点一点地调试/修复代码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-